4

I'm writing a Python code to read the points in a polygon shapefile and save them to a point shapefile.

I'm not sure about my approach so if you have a better idea (even if it is totally different from mine) please let me know.

So first I made a text file and stored the points' (x,y) in that .txt file. then I tried to make a point shapefile from the text file but it gave an error.

Here is the code (just the last part):

creat point shape-file from text file 
import fileinput
import string
import os
env.overwriteOutput=True
outpath="C:/roadpl"
newfc="newpoint.shp" 
arcpy.CreateFeatureclass_management(outpath, newfc, "Point")
infile="C:/roadpl/roadL5.txt"
cursor=arcpy.da.InsertCursor(newfc, ["SHAPE@"])
array=arcpy.Array()
for line in fileinput.input(infile):
    point.X, point.Y = line.split() 
    line_array.add(point)
cursor.insertRow([arcpy.Point(array)])
fileinput.close()
del cursor

Here is the error:

Traceback (most recent call last):
  File "C:\roadpl\P_Code_L5", line 49, in <module>
    point.X, point.Y  = line.split()
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\_base.py", line 87, in _set
    return setattr(self._arc_object, attr_name, cval(val))
RuntimeError: Point: Input value is not numeric

I made the input file (.txt file) from a polygon shapefile using:

cur = arcpy.SearchCursor("C:/roadpl/L5P.shp", ["SHAPE@"])
f=open("C:/roadpl/roadL5.txt", "w")

for row in cur:
    geom = row.shape
    row = geom.getPart()
    for part in row:
        for point in part:
          print >>f, (point.X, point.Y)
f.close()
nmtoken
  • 13,355
  • 5
  • 38
  • 87
user2841098
  • 185
  • 2
  • 8

2 Answers2

7

Your X and Y values are text and need to be converted to numeric values. You can tweak your code as follows:

for line in fileinput.input(infile):
    X,Y=line.split()
    cursor.insertRow([arcpy.Point(float(X),float(Y))])

EDIT
I had been focusing on the fact that your input was text but on closer inspection of your code I think I have spotted another error. As I understand it, you want to import each pair of coordinates as a separate point. At the moment you are adding all the points into a single array and then trying to define that whole array as a point. A point can only have two coordinates. So, see the revised code above.

MappaGnosis
  • 33,857
  • 2
  • 66
  • 129
  • So you mean I have to replace: point.X, point.Y = line.split() line_array.add(point) ? I did and it gave me this error: NameError: name 'string' is not defined – user2841098 Oct 03 '13 at 14:19
  • I don't see that line in your code in your question. In the code I do see you are reading strings from a text file and they need to be converted to their numeric equivalents. – MappaGnosis Oct 03 '13 at 14:35
  • I changed the original code to show the error, I changed it back to the original code, when I used your two lines it gave another error. I'm new to Python so the error might be really easy to fix but i dont get it: " NameError: name 'string' is not defined" – user2841098 Oct 03 '13 at 14:51
  • @MappaGnosis, is there a reason to use eval() instead of float() (I think they are identical for this usage)? They take the same amount of time (according to timeit), so I was wondering if it's just user preference. – Paul Oct 03 '13 at 14:58
  • See my edited answer. Float is fine too and often considered safer if you have user input involved. – MappaGnosis Oct 03 '13 at 14:59
  • @MappaGnosis Thank you but I still cannot run it without error and now i have a more important question: Is there any other way to read points from polygon and make a point shape file out of them and find the min X and max X? I think my approach might be totally wrong since I'm new to Python. So beside this error, how do you do it? – user2841098 Oct 10 '13 at 02:52
  • Can you edit your question to show the error you are now getting. As for your other question, I think I saw that you had asked that separately (which is the best way to do it). I'll have a look at your other post. – MappaGnosis Oct 10 '13 at 07:32
  • See my edits - I have spotted another problem with your code – MappaGnosis Oct 10 '13 at 07:40
0
RuntimeError: Point: Input value is not numeric

it seems that the problem is in your input file.

Gery
  • 2,135
  • 1
  • 15
  • 37
  • I made the input file (.txt file) from a polygone shapefile using:
    cur = arcpy.SearchCursor("C:/roadpl/L5P.shp", ["SHAPE@"])
    

    f=open("C:/roadpl/roadL5.txt", "w")

    for row in cur:

    geom = row.shape
    
    
    row = geom.getPart()
    
    
    for part in row:
        for point in part:
          print >>f, (point.X, point.Y)
    

    f.close()

    – user2841098 Oct 03 '13 at 14:00