Select to view content in your preferred language

ArcPy imported points not displaying in correct location

1428
7
Jump to solution
08-18-2014 08:59 AM
NettoChad
New Contributor III

I'm using Python to read the points and add them to an existing shape file. The problem is that the all the points are in the same location and no where the location they are supposed to be. NOTE: If I calculate the X and Y fields, all the correct coordinates are displayed. If export the data and import back in, the points are in the correct location. Does anyone have any suggestions on fixing this?

import arcpy

arcpy.overwriteoutput = True

out_path = r"M:\work\nettoc\MAS201300311\PythonModule\import_data"

pointFC = r"M:\work\nettoc\MAS201300311\PythonModule\import_data\POI.shp"

geoType = "POINT"

StructurePoints = open(r"M:\work\nettoc\MAS201300311\PythonModule\import_data\TEST2.txt", "r")

coordList = []

# Parse the exported text file and create a list that Python can read

# figure out position of the lat and long in the header

headerLine = StructurePoints.readline()

valueList = headerLine.split(",")

latValueIndex = valueList.index('"SURF_LATITUDE"')

longValueIndex = valueList.index('"SURF_LONGITUDE"')

planValueIndex = valueList.index('"PLAN"')

typeValueIndex = valueList.index('"PLAN_SITE_TYPE"')

ancRadiusValueIndex = valueList.index('"ANCHOR_RADIUS"')

# Read line in the file and append to coordinate list

for line in StructurePoints.readlines():

    # need to say what the seperating value is, in this case its the ","

    segmentedLine = line.split(",")

    # only append the value (index) indicated... we could have used "segmentedline[2]", but if lat changes position in the header list

    # we would have to change the index number.

    coordList.append([segmentedLine[planValueIndex], segmentedLine[typeValueIndex], segmentedLine[ancRadiusValueIndex], segmentedLine[longValueIndex], segmentedLine[latValueIndex]])

# Loop and delete any existing features in the shapefile

rows = arcpy.UpdateCursor(pointFC)

for row in rows:

    rows.deleteRow(row)

del rows, row

print "Prepairing shape file..."

# Loop through new created coordlist and insert the the points in the existing shape file

rowInserter = arcpy.InsertCursor(pointFC)

#Loop through each coordinate in the list

for coordinate in coordList:

    # Grab a set of coordinates from the list and assign them to a point obejct

    longValue = float(coordinate[3])

    latValue = float(coordinate[4])

    pointGeometry = arcpy.Point(longValue,latValue)

    # use the insert cursor to put in the point object in the feature class

    newPoint = rowInserter.newRow()

    newPoint.Shape = pointGeometry

    newPoint.DESCR = coordinate[0] + ", " + coordinate[1]

    newPoint.ANCHOR_RAD = coordinate[2]

    rowInserter.insertRow(newPoint)

del rowInserter

print "The following points were created"

print coordList

Tags (4)
0 Kudos
1 Solution

Accepted Solutions
NettoChad
New Contributor III

Thank you guys for the help, It ended up being the projection of the shapefile I was modifying. It was different from the mxd.

@Neil Ayres...thank your for your suggestion, I ended up using "arcpy.PointGeometry" and adding a Describe variable in the beginning of the code. I like the fact that it adds the SR to the points.

View solution in original post

0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

Neto...what was the coordinate system of the shapefile that you are adding the points to?  are you adding this file to its own dataframe after running the script or is the file in arcmap already?

0 Kudos
NettoChad
New Contributor III

The shapefile is in the ........... coordinate system

The file is already in a MXD file, the script deletes all existing points and adds the new ones.

Thank your responding

0 Kudos
NettoChad
New Contributor III

Sorry its in the "NAD_1927_StatePlane_Louisiana_Offshore_FIPS_1703" coordinate system.

0 Kudos
NettoChad
New Contributor III

Thank you guys for the help, It ended up being the projection of the shapefile I was modifying. It was different from the mxd.

@Neil Ayres...thank your for your suggestion, I ended up using "arcpy.PointGeometry" and adding a Describe variable in the beginning of the code. I like the fact that it adds the SR to the points.

0 Kudos
FilipKrál
Occasional Contributor III

Hmm, I cannot see anything obviously wrong with your code so it might be something about your data.

Did you verify that your longValue and latValue variables contain what you would expect for example using a print statement?

Below is your code where the inserting uses the InsertCursor in the data access module (but haven't tested it). If you have ArcGIS 10.1 and above, try it.

Filip.

import arcpy

arcpy.env.overwriteOutput = True

out_path = r"M:\work\nettoc\MAS201300311\PythonModule\import_data"

pointFC = r"M:\work\nettoc\MAS201300311\PythonModule\import_data\POI.shp"

geoType = "POINT"

StructurePoints = open(r"M:\work\nettoc\MAS201300311\PythonModule\import_data\TEST2.txt", "r")

coordList = []

# Parse the exported text file and create a list that Python can read

# figure out position of the lat and long in the header

headerLine = StructurePoints.readline()

valueList = headerLine.split(",")

latValueIndex = valueList.index('"SURF_LATITUDE"')

longValueIndex = valueList.index('"SURF_LONGITUDE"')

planValueIndex = valueList.index('"PLAN"')

typeValueIndex = valueList.index('"PLAN_SITE_TYPE"')

ancRadiusValueIndex = valueList.index('"ANCHOR_RADIUS"')

# Read line in the file and append to coordinate list

for line in StructurePoints.readlines():

    # need to say what the seperating value is, in this case its the ","

    segmentedLine = line.split(",")

    # only append the value (index) indicated... we could have used "segmentedline[2]", but if lat changes position in the header list

    # we would have to change the index number.

    coordList.append([segmentedLine[planValueIndex], segmentedLine[typeValueIndex], segmentedLine[ancRadiusValueIndex], segmentedLine[longValueIndex], segmentedLine[latValueIndex]])

# Loop and delete any existing features in the shapefile

rows = arcpy.UpdateCursor(pointFC)

for row in rows:

    rows.deleteRow(row)

del rows, row

print "Prepairing shape file..."

# Loop through new created coordlist and insert the the points in the existing shape file

with arcpy.da.InsertCursor(pointFC, ["SHAPE@", "DESCR", "ANCHOR_RAD"]) as rowInserter:

    #Loop through each coordinate in the list

    for coordinate in coordList:

        # Grab a set of coordinates from the list and assign them to a point obejct

        longValue = float(coordinate[3])

        latValue = float(coordinate[4])

        pointGeometry = arcpy.Point(longValue,latValue)

        descr = coordinate[0] + ", " + coordinate[1]

        andchor_rad = coordinate[2]

        newrow = [pointGeometry, descr, anchor_rad]

        rowInserter.insertRow(newrow)

del rowInserter

print "The following points were created"

print coordList

0 Kudos
NeilAyres
MVP Alum

Not sure if this will answer the question but...

When creating the geometry object, it is vital (esp with GCS coords) to provide a spatial reference as well.

So line 53 above could be changed to :

pointGeometry = arcpy.PointGeometry(arcpy.Point(longValue,latValue), SR)

Or something like that anyway. Get the SR from the Describe object of the created feature class.

This has been a source of some confusion for me in the past in that arcpy has 2 sorts of "point" geometries.

Those created by arcpy.Point(X,Y) which can be inserted into polyline or polygon geometries (at which point the SR is applied), and those created by arcpy.PointGeometry(arcpy.Point([X,Y]), SR). The help files are not particularly good at explaining the difference.

Cheers,

Neil

0 Kudos
NettoChad
New Contributor III

Filip Král‌ I am running ArcGIS 10.0, so Im not able to use the "new" insert cursor (da).

0 Kudos