Follow us on Twitter

Using ImportData

Share:

Some of the most complex and difficult to use methods in MapPoint’s programming interface (API) are those that import data and plot it. This article, and its companion demonstrate how to perform these operations using C#. Finally, a third article demonstrates how to perform these calls with Python.

This article looks at the ImportData method, demonstrating how it should be used. Samples use C# code with MapPoint 2010, and a corrected version of the Sales.txt file that can be found in MapPoint’s Samples directory. This can be downloaded here.

The API equivalent of the Data Import Wizard is the Datasets.ImportData method. Despite the complexities of the import wizard, this method only requires five parameters:

dsMoniker is a text string that holds the name of the input data file. A text file simply requires a pathname, but Excel and Access files can include worksheet or table information. The syntax for these extensions is described in the MapPoint documentation.

arrayOfFields is a two column array that specifies the data fields and how they are to be interpreted. Each row in this array represents a data field. The first column lists the field names. The second column is typically a GeoFieldType enum that specifies how the column is to be interpreted/geocoded; a string (the name for the data field in MapPoint); or blank (let MapPoint decide what to do).  MapPoint will attempt to import and interpret all data fields if this parameter is not supplied.

eCountry is an optional GeoCountry enum value that specifies the country. Default is geoCountryDefault.

eDelimiter is an optional GeoDelimiter value that specifies the field delimiter for text files. It is ignored for other file types.  Acceptable values are geoDelimiterDefault, geoDelimiterComma, geoDelimiterSemicolon, and geoDelimiterTab.

Finally, nImportFlags is an optional long integer value. This is an ‘OR’ combination of GeoImportFlags  values that specify details about the dsMoniker specification for Access and Excel data, and whether the first data row contains field headings or not.

Here is a C# console example that loads the Sales.txt sample data:

This example starts a MapPoint application instance, and loads the sales data. It also queries and displays the available fields in the new dataset – this is primarily for diagnostic purposes. It will prove useful when we are trying to create a data map from the data. The data is loaded as a simple pushpin set, but the DisplayDataMap method can be used to change this.

The call to ImportData sets the country to the US, and that the text file is tab-separated with headings defined in the first row. The required data fields are also explicitly listed (in the fieldSpecifications array). The main thing you have to be wary of is that the field specifications have to be exact. A mis-spelt or mis-specified field column can result in a field being skipped, or an exception (error) being thrown.

If you are not sure what the fields are, you can let MapPoint decide. Do this by passing  System.Reflection.Missing.Value (variable ‘missing’) instead of the field specifications array:

This produces an identical result to the original code with the explicit field specifications. In the general case, it will read all available fields, and MapPoint will try to decide which hold useful geographic information.  Geographic information will only be correctly recognized if you use standard names (e.g. “Longitude”). In general, it is best to provide the field specifications so that all geographic fields are correctly recognized.

This example is partially based on the “Working with Your Business Data” samples in chapter 3 of Chandu Thota’s “Programming MapPoint .NET”. Readers interested in the correct use of Excel worksheets in ImportData are referred to this chapter for further information and sample code.

2 comments to Using ImportData

  • Matt Fritz

    FindAddressResults returns a FindResults collection. When calling ImportData, is there a collection of unmatched addresses available to the programmer? We see this collection when using the UI Import Data function but ImportData does appear to return anything. How can the programmer know the results?

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">