GEOG 863
GIS Mashups for Geospatial Professionals

Converting a Shapefile to XML

PrintPrint

Part of the database-driven approach to web mapping that will be demonstrated here is to output the data retrieved from the database in XML format.  XML is often used in this way as a language- and platform-independent way to pass data between applications.  As an example, here are the Jen and Barry's candidate city data stored in XML format.  And here is an alternate way of storing these same data.

Either form can be used to supply data for a mashup. However, I recommend using the first form for two reasons:

  • It requires slightly less storage space (314 bytes vs. 497 bytes in this case), which results in a faster loading time for your mashup.
  • The JavaScript code required to read the data is a bit less complex.

So, how do you go about putting the data into this format?  Before we see how to do that in a database context, let's ease our way into the usage of XML in web mapping by working with a tool that I wrote for ArcToolbox.  This tool can be used to export any point, line or polygon shapefile into the format shown above.  This part of the lesson will guide you through the installation and usage of this tool.

  1. Download the mashup_tools.zip file from our server.
  2. Unzip the folder so that all the files are sitting directly under:

    XP/Win2000: \Documents and Settings\<your user name>\Application Data\ESRI\ArcToolbox\My Toolboxes
    
    Vista or Windows 7/8 (make sure you have hidden files and folders displayed): \Users\<user name>\AppData\Roaming\ESRI\Desktop<version>\ArcToolbox\My Toolboxes
    You should see at least three files: Mashup Tools.tbx, shp2xml.py and shp2csv.py.
  3. Open ArcMap and the Catalog window.
  4. In the Catalog window, browse to Toolboxes > My Toolboxes > Mashup Tools.

    Note

    I used the Python programming language to create this tool. If you're interested in learning more about automating geoprocessing tasks with Python, check out our course on the topic (GEOG 485).

Using the tool

The Mashup Tools toolbox contains a tool called Export Shapefile to XML. Its usage is straightforward, though here are some details, using the candidate_cities shapefile as an example:

  1. Add the candidate_cities shapefile to ArcMap.
  2. Open the Mashup Tools toolbox and double-click on the Export Shapefile to XML tool to open it.
  3. The Layer drop-down list will show all of the feature layers in the active data frame. Select the candidate_cities layer. (You could also click on the Browse icon to choose a shapefile not currently loaded in the active data frame.)
  4. After selecting a layer, the Fields drop-down list will display all of the fields in the selected layer. Select the NAME field from the list to make it appear in the selection below.
  5. Repeat the previous step for the POPULATION field and any other fields you want to include in the output.
  6. Click OK to perform the export. The output file will be written to the same folder that holds the input shapefile. Its name will match that of the shapefile with a .xml suffix (in this case, candidate_cities.xml).
  7. Open the output XML file in a web browser and confirm that it includes all of the desired data. The last two attributes of each record in the file will be the x and y coordinates of the point.

    Note

    When you use the tool on a line or polygon layer, the x and y attributes represent the centroid of the feature. Beneath each feature is then a list of the vertices that delineate that feature. For example, here are the coordinates of Pennsylvania's counties in XML format.

Now that you've seen how to convert GIS data to XML format, let's see how to write JavaScript that reads point data from an XML file and adds markers to a Google Map.