Select to view content in your preferred language

How to read out coordinates of a txt file and create a shapefile

711
2
Jump to solution
07-03-2013 09:52 PM
YvonneDeus
Emerging Contributor
Hi I have already, the coordinates:

import arcpy import re  from itertools import product pnt = arcpy.Point() KEY_VALUE_RE = re.compile(r'([a-z_][a-z0-9_]*)=(\d+(?:\.\d*)?)')  def main():     points_to_search_r = [         'p{0}_{1}w'.format(a, b) for a, b in product([1, 2, 4, 6, 7, 9], 'r')     ]     points_to_search_h = [         'p{0}_{1}w'.format(a, b) for a, b in product([1, 2, 4, 6, 7, 9], 'h')     ]           with open('F:\\Rasterdaten\\SRV\\2005\\TK100\\c4706_3.txt') as lines:         for line in lines:             variables = dict(                 (k, float(v)) for k, v in KEY_VALUE_RE.findall(line)             )             for point_r in points_to_search_r:                 value_r = variables.get(point_r)                 if value_r is not None:                     #print '{0} {1}'.format(point_r, value_r)                     pnt.Y = value_r                     #print pnt.Y                                 for point_h in points_to_search_h:                 value_h = variables.get(point_h)                 if value_h is not None:                     #print '{0} {1}'.format(point_h, value_h)                     pnt.X = value_h                     #print pnt.X                     if __name__ == '__main__':     main()


And I have a code for read out coordinates of a dbf file and create a shapefile:

import arcpy  arcpy.env.overwriteOutput=1 arcpy.env.workspace = "E:\\mxd\\Shapefiles\\" arcpy.CreateFeatureclass_management(arcpy.env.workspace, "test_polygon.shp","POLYGON") cur=arcpy.InsertCursor("test_polygon.shp") lineArray = arcpy.Array() pnt = arcpy.Point() cur2 = arcpy.SearchCursor("F:\Rasterdaten\SRV\\gps_points.dbf")  for row in cur2:      pnt.X = row.getValue("x")     pnt.Y = row.getValue("y")      lineArray.add(pnt)  feat = cur.newRow() feat.shape = lineArray cur.insertRow(feat) print "Berechnung beendet"  del cur del cur2 del row


How can I combine this two?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ArkadiuszMatoszka
Frequent Contributor
Hi,

Depends what kind of geometry you want to create. In your second script it's polyline. If you want to keep it that way code would be:

import arcpy import re  from itertools import product pnt = arcpy.Point() KEY_VALUE_RE = re.compile(r'([a-z_][a-z0-9_]*)=(\d+(?:\.\d*)?)') lineArray = arcpy.Array()  def main():  i_cur=arcpy.InsertCursor("test_polygon.shp") # create cursor to insert features  points_to_search_r = [   'p{0}_{1}w'.format(a, b) for a, b in product([1, 2, 4, 6, 7, 9], 'r')  ]  points_to_search_h = [   'p{0}_{1}w'.format(a, b) for a, b in product([1, 2, 4, 6, 7, 9], 'h')  ]     with open('F:\\Rasterdaten\\SRV\\2005\\TK100\\c4706_3.txt') as lines:   for line in lines:    pnt.X = 0.0 # reset values for each line    pnt.Y = 0.0    variables = dict(     (k, float(v)) for k, v in KEY_VALUE_RE.findall(line)    )    for point_r in points_to_search_r:     value_r = variables.get(point_r)     if value_r is not None:      #print '{0} {1}'.format(point_r, value_r)      pnt.Y = value_r      #print pnt.Y            for point_h in points_to_search_h:     value_h = variables.get(point_h)     if value_h is not None:      #print '{0} {1}'.format(point_h, value_h)      pnt.X = value_h      #print pnt.X    if pnt.X != 0.0 and pnt.Y != 0.0:     lineArray.add(pnt) # add point to polyline  feat = i_cur.newRow()  feat.SHAPE = lineArray  i_cur.insertRow(feat)  del i_cur if __name__ == '__main__':  main()


If you want to create points, code will be:


import arcpy import re  from itertools import product pnt = arcpy.Point() KEY_VALUE_RE = re.compile(r'([a-z_][a-z0-9_]*)=(\d+(?:\.\d*)?)') #lineArray = arcpy.Array()  def main():  i_cur=arcpy.InsertCursor("test_polygon.shp") # create cursor to insert features  points_to_search_r = [   'p{0}_{1}w'.format(a, b) for a, b in product([1, 2, 4, 6, 7, 9], 'r')  ]  points_to_search_h = [   'p{0}_{1}w'.format(a, b) for a, b in product([1, 2, 4, 6, 7, 9], 'h')  ]     with open('F:\\Rasterdaten\\SRV\\2005\\TK100\\c4706_3.txt') as lines:   for line in lines:    pnt.X = 0.0 # reset values for each line    pnt.Y = 0.0    variables = dict(     (k, float(v)) for k, v in KEY_VALUE_RE.findall(line)    )    for point_r in points_to_search_r:     value_r = variables.get(point_r)     if value_r is not None:      #print '{0} {1}'.format(point_r, value_r)      pnt.Y = value_r      #print pnt.Y            for point_h in points_to_search_h:     value_h = variables.get(point_h)     if value_h is not None:      #print '{0} {1}'.format(point_h, value_h)      pnt.X = value_h      #print pnt.X    if pnt.X != 0.0 and pnt.Y != 0.0:     feat = i_cur.newRow()     feat.SHAPE = pnt     i_cur.insertRow(feat)  del i_cur if __name__ == '__main__':  main()


Hope it will help.

Regards
Arek

View solution in original post

0 Kudos
2 Replies
ArkadiuszMatoszka
Frequent Contributor
Hi,

Depends what kind of geometry you want to create. In your second script it's polyline. If you want to keep it that way code would be:

import arcpy import re  from itertools import product pnt = arcpy.Point() KEY_VALUE_RE = re.compile(r'([a-z_][a-z0-9_]*)=(\d+(?:\.\d*)?)') lineArray = arcpy.Array()  def main():  i_cur=arcpy.InsertCursor("test_polygon.shp") # create cursor to insert features  points_to_search_r = [   'p{0}_{1}w'.format(a, b) for a, b in product([1, 2, 4, 6, 7, 9], 'r')  ]  points_to_search_h = [   'p{0}_{1}w'.format(a, b) for a, b in product([1, 2, 4, 6, 7, 9], 'h')  ]     with open('F:\\Rasterdaten\\SRV\\2005\\TK100\\c4706_3.txt') as lines:   for line in lines:    pnt.X = 0.0 # reset values for each line    pnt.Y = 0.0    variables = dict(     (k, float(v)) for k, v in KEY_VALUE_RE.findall(line)    )    for point_r in points_to_search_r:     value_r = variables.get(point_r)     if value_r is not None:      #print '{0} {1}'.format(point_r, value_r)      pnt.Y = value_r      #print pnt.Y            for point_h in points_to_search_h:     value_h = variables.get(point_h)     if value_h is not None:      #print '{0} {1}'.format(point_h, value_h)      pnt.X = value_h      #print pnt.X    if pnt.X != 0.0 and pnt.Y != 0.0:     lineArray.add(pnt) # add point to polyline  feat = i_cur.newRow()  feat.SHAPE = lineArray  i_cur.insertRow(feat)  del i_cur if __name__ == '__main__':  main()


If you want to create points, code will be:


import arcpy import re  from itertools import product pnt = arcpy.Point() KEY_VALUE_RE = re.compile(r'([a-z_][a-z0-9_]*)=(\d+(?:\.\d*)?)') #lineArray = arcpy.Array()  def main():  i_cur=arcpy.InsertCursor("test_polygon.shp") # create cursor to insert features  points_to_search_r = [   'p{0}_{1}w'.format(a, b) for a, b in product([1, 2, 4, 6, 7, 9], 'r')  ]  points_to_search_h = [   'p{0}_{1}w'.format(a, b) for a, b in product([1, 2, 4, 6, 7, 9], 'h')  ]     with open('F:\\Rasterdaten\\SRV\\2005\\TK100\\c4706_3.txt') as lines:   for line in lines:    pnt.X = 0.0 # reset values for each line    pnt.Y = 0.0    variables = dict(     (k, float(v)) for k, v in KEY_VALUE_RE.findall(line)    )    for point_r in points_to_search_r:     value_r = variables.get(point_r)     if value_r is not None:      #print '{0} {1}'.format(point_r, value_r)      pnt.Y = value_r      #print pnt.Y            for point_h in points_to_search_h:     value_h = variables.get(point_h)     if value_h is not None:      #print '{0} {1}'.format(point_h, value_h)      pnt.X = value_h      #print pnt.X    if pnt.X != 0.0 and pnt.Y != 0.0:     feat = i_cur.newRow()     feat.SHAPE = pnt     i_cur.insertRow(feat)  del i_cur if __name__ == '__main__':  main()


Hope it will help.

Regards
Arek
0 Kudos
YvonneDeus
Emerging Contributor
Thanks 🙂

It is for polygons!
0 Kudos