Monday, February 12, 2007

Python Script: Shape to Text to Shape

Except for ironing out a few remaining buglets and cleaning up the code, I have just about completed a script that will convert a shapefile of any geometry into a text file, and then will convert that text file back into a shapefile. (I previously wrote about the development of this script here and here.)

Script configured for ArcGIS 9.1: Download
This will not work for ArcGIS 9.0 and has not yet been tested for 9.2. For a narrative of my ArcGIS 9.2 woes, go here.

Purpose

This script has two potential uses: Edit an existing shapefile or Create a new shapefile in text format.

Description

There are actually two scripts: shp2exch.py and exch2shp.py that are launched independent of each other.

I initially developed the script so that faculty in our Earth & Environmental Science department can disassemble shapefiles, apply tectonic rotation formulas to the resulting text files, and then reassemble the shapefiles.
  • shp2txt converts points, polylines (including multipart), and polygons (including multipart and inner circles) to a text file using an exchange format. The text file can be edited by hand or of course another application can be developed to edit these text files.
  • txt2shp converts an existing text file in the exchange format to a shapefile.
Instructions

Download and unzip the contents. Open ArcMap or ArcCatalog 9.1. View the ArcToolbox pane. Right-click on ArcToolbox and select Add Toolbox. Browse to the directory where you unzipped the scripts, and click once on exchanger, and click Open.

Now you can expand the exchanger toolbox to launch the two scripts.

There are also two executable files that launch forms external of ArcGIS. However, ArcGIS still needs to be installed on the system and everything runs slower. You can give it a shot, but I advise using the toolbox within ArcGIS.

The Exchange Format

The text file uses a CSV comma delimited (comma-separated values) file structure with a extension. I used a .csv extension so that the file can be launched directly into Excel by double-clicking.

The text file uses an exchange format that follows the following structure:
  • First line contains all of the attribute (field) names in the shapefile, plus xLatitude, xLongitude, and geometry
  • Second line contains the field type of the attributes
  • The third line through the last line contain the X and Y value for each vertex.
    • In addition, each new feature, part, or inner circle
      is prefaced with a line beginning with NEW and the attributes for that feature or part.
Here is an example of a text file representing a U.S. states polygon shapefile. Note the multipart polygon of Hawaii.
Here is an example of a text file representing freeway lines in North Central Texas.

Known Issues
  1. Coordinate system is preserved because the XY units are preserved in the text file. However, I have been having trouble preserving the complete projection information, so the resulting shapefile projections are not defined.
  2. Currently only works with shapefiles, and not geodatabase feature classes. This is due to poor planning as the script currently relies on the FID field, and does not look for an OBJECTID field.
  3. Probably does not work with ArcGIS 9.2. After last week's troubles, I do not expect to install and fix any 9.2 bugs within the next couple of weeks.
  4. Code is currently a bit sloppily written. Needs to be cleaned up a bit.

7 comments:

Anonymous said...

Provided I add the toolbox to ArcCatalog the export from shp to exchange format works great under ArcView 9.2! The compiled Windows tool gave me the error: File not found. Haven't looked into what is going wrong here.

cheers,
Mark

mapz said...

Wow, that's fantastic that the scripts work in 9.2. I was a bit dubious about the compatibility. Thanks for letting me know.

As to the compiled VB form, I am unsure what happened. Could it perhaps be a different version of the .NET framework? Not sure how useful it is to run the scripts outside of ESRI anyway. Think I will remove the VB forms and upload the scripts and toolbox to ArcScripts.

Thanks again!

Unknown said...

I tried to download the script but got a 404 error.

Thanks

Tim, Forestry UBC

Anonymous said...

Also 404 error for me, I hope it will be fixed as I quite interested on such scritp.

Upss

Anonymous said...

My name is Peter Zalavari. I come from Hungary and now I make an intership by Z_GIS (www.uni-salzburg.at/zgis).
I'm working on a python script to convert text files into point shapefiles.
I tried to download your script, but got a 404 error. If your script is free to modify, send me please. I would like to write a new one, but your script would be a good starting point for me.
Thanks!

Cheers,
Peter

Anonymous said...

the files don't seem to be there anymore. still getting a 404 error

jasonbob said...

jordans
supreme new york
nike shox for women
curry shoes
pandora jewelry
bape clothing
balenciaga sneakers
golden goose
golden goose
pg shoes