Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

Code converting lat/long coordinates (WKT) to polygon (shapefile) not working

$
0
0

I am having a problem converting WKT to shapefile. I have a csv file consisting of three columns: ID, Name (of the area) and WKT comprising set of lat/long coordinates of area boundaries. For example, first row looks like this:

1700003504,Westbourne Green,”(51.5212 -0.2011,51.5213 -0.2033,51.5217 -0.2032,51.5217 -0.204,51.524 -0.2039,51.524 -0.2046,51.5258 -0.2045,51.5258 -0.2024,51.5253 -0.2024,51.5253 -0.2009,51.5248 -0.201,51.5248 -0.1995,51.5244 -0.1995,51.5244 -0.1988,51.5239 -0.1988,51.5239 -0.1981,51.523 -0.1982,51.523 -0.1989,51.5226 -0.1989,51.5226 -0.1996,51.5221 -0.1996,51.5221 -0.2003,51.5217 -0.2004,51.5217 -0.2011,51.5212 -0.2011)”

I stumbled upon the code online which makes use of ArcPy’s FromWKT function, but it does not appear to work for some reason. The code is as it follows:

# convert well known text to geometry, and compile shapes into a single feature     class...
# 11/15/2012
import arcpy

#reference: https://geonet.esri.com/thread/63090


File = "H:\...\ClgBoundaries1.csv"

# dimension the WKT string field and poly ID field...
# the field holding the WKT string...
field1 = "Wkt"
# the field holding the unique ID...
field2 = "Id"
#the field holding the name
field3 = "NAME"

# set up the empty list...
featureList = []

#spatialreference WGS1984
sr = arcpy.SpatialReference(4326)

# iterate on table row...
cursor = arcpy.SearchCursor(File)
row = cursor.next()
while row:
  print (row.getValue(field2))
  print (row.getValue(field3))
  wkt = row.getValue(field1)
  # this is the part that converts the WKT string to geometry...
  temp = arcpy.FromWKT(wkt, "")
  # append the current geometry to the list...
  featureList.append(temp)
  row = cursor.next()

# copy all geometries in the list to a feature class...
arcpy.CopyFeatures_management(featureList,        "H:\...\ClgBoundaries.shp")    

# clean up...
del row, temp, WKT, File, field1, featureList, cursor

Textual output of the code are only first area’s ID and first area’s name,
1700003504
Westbourne Green
but nothing else happens. This error message shows:

Traceback (most recent call last):
File "H:...WktConversion.py", line 32, in <module>
temp = arcpy.FromWKT(wkt, "")
File "C:Program Files (x86)ArcGISDesktop10.2arcpyarcpy__init__.py",    line 1627, in FromWKT
return gp.createObject("_wkt", wkt_string, spatial_reference)
File "C:Program Files    (x86)ArcGISDesktop10.2arcpyarcpygeoprocessing_base.py", line 379, in createObject
self._gp.CreateObject(*gp_fixargs(args, True)))
RuntimeError: Object: CreateObject _wkt not valid

What should I do to successfully convert WKTs to SHP polygons?


Viewing all articles
Browse latest Browse all 767

Latest Images

Trending Articles



Latest Images