Adding XY Coordinates from a table (.csv) file into ArcMap using Python

2587
8
Jump to solution
05-01-2020 08:20 PM
ShalinR
New Contributor II

I do not where I went wrong here. Please help me. 

import arcpy
import fileinput
import string
import os
from arcpy import env
env.workspace = "P:\GEOG - Programming in GIS\Final project\Data"
env.overwrightOutput = True
outpath = "P:\GEOG - Programming in GIS\Final project\Data"
newfc= "Results/NewPtssss.shp"
arcpy.CreateFeatureclass_management(outpath, newfc, "Point")
infile = "P:\GEOG - Programming in GIS\Final project\Data\coordinates.txt"
cursor = arcpy.da.InsertCursor(newfc, ["SHAPE@"])
array = arcpy.Array()
for point in fileinput.input(infile):
if ":" in point:
x, y = string.strip().split(":")
array.add(arcpy.Point(x, y))
cursor.insertRow([arcpy.Point(array)])
fileinput.close()
del cursor

I get the following error:
Traceback (most recent call last):
File "P:/GEOG - Programming in GIS/XYToPoint_Practice.py", line 18, in <module>
cursor.insertRow([arcpy.Point(array)])
File "P:\ArcMap\Desktop10.7\ArcPy\arcpy\arcobjects\mixins.py", line 1122, in __init__
setattr(self, attr, value)
File "P:\ArcMap\Desktop10.7\ArcPy\arcpy\arcobjects\_base.py", line 89, in _set
return setattr(self._arc_object, attr_name, cval(val))
RuntimeError: Point: Input value is not numeric

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus
pnt = "1.1:2.2"

coords = pnt.strip().split(":")

x, y = [float(val) for val in coords]‍‍‍‍‍

x, y
(1.1, 2.2)

For your immediate problem, you weren't splitting the point (pnt) into its constituent parts properly,

python's "split" returns a list of string values,

They need to be converted to 'float' values before you pass them on to arcpy's Point object.

View solution in original post

8 Replies
DanPatterson_Retired
MVP Emeritus

/blogs/dan_patterson/2016/08/14/script-formatting 

Format your code so indentation can be checked and people can answer with respect to line numbers.

DanPatterson_Retired
MVP Emeritus
pnt = "1.1:2.2"

coords = pnt.strip().split(":")

x, y = [float(val) for val in coords]‍‍‍‍‍

x, y
(1.1, 2.2)

For your immediate problem, you weren't splitting the point (pnt) into its constituent parts properly,

python's "split" returns a list of string values,

They need to be converted to 'float' values before you pass them on to arcpy's Point object.

ShalinR
New Contributor II

Sorry about the formatting. Never previously posted a question of this nature. Thanks for the reply. So am I replacing the code example you provided for line 14? 

0 Kudos
Katie_Clark
MVP Regular Contributor

One quick thing I'm not sure if you already noticed (it's not directly related to your error message), but you have a typo in the beginning....should be env.overwriteOutput, not env.overwrightOutput   

Best,
Katie


“The goal is not simply to ‘work hard, play hard.’ The goal is to make our work and our play indistinguishable.”
- Simon Sinek
ShalinR
New Contributor II

Thanks for catching my mistake. So I ended up using another script, which I'm attaching to the reply, and I got no errors. But the problem now is that the point shapefile prints points nowhere near the shapefile of the state that I am trying to plot them onto. 

0 Kudos
ShalinR
New Contributor II

Whoops, made a mistake with latitude and longitude, will let you know if that was the mistake I made. 

0 Kudos
ShalinR
New Contributor II

That was it. All the points are where I needed them to be. Thanks for all your help Katherine Clark @Dan Patterson

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Please mark a reply as Correct or mark the thread as assumed answered to close it out.  Also, make sure to mark Helpfuls for comments that were, well, helpful.

0 Kudos