I currently have a code that creates a point with a mouse click and works great, but i need the x,y to be calculated to lat and long. The current codes does that but it is slow and i use updatecursor. I would like to like to do the conversion when the ptGeometry, basically store it in the point when it gets created because with the current code it takes about 30 secs. I have tried to makefeaturelayer with the ptGeometry but and insert the lat long fields and then use the to update the point but i get nothing. so i need some help on how i can do this with the current code.
#import modules
import arcpy
"""
tool box parameters
point param = feature set
targetpoint = featurelayer
parcel = featurelayer
"""
arcpy.env.qualifiedFieldNames = False
point = arcpy.GetParameterAsText(0) #point boolen
targetpoint = "CCAP" #target point feature class
parcel = "parcels" #target feature class
parcel_lyr = 'parcel_lyr'
for prow in arcpy.da.SearchCursor(point,'SHAPE@XY'):
x,y = prow[0]
del prow
point1 = arcpy.Point(x, y)
ptGeometry = arcpy.PointGeometry(point1)
CC_list = []
with arcpy.da.SearchCursor(targetpoint, ["AddressID"]) as cursor:
for row in cursor:
try:
if "CC" in row[0]:
CC_list.append(int(row[0].strip("CC")))
except TypeError:
pass
del cursor
CC_list.sort()
AddressID = CC_list[-1] + 1
AddressID = 'CC' + str(AddressID)
arcpy.MakeFeatureLayer_management(parcel,parcel_lyr)
arcpy.SelectLayerByLocation_management(parcel_lyr,"INTERSECT",ptGeometry)
fldList = ['ACCOUNT','OwnerName','SiteAddres','SiteNum','siteNumSfx','Predir','SiteStreet', 'StreetType', 'Postdir', 'SiteCity', 'SiteZip']
fldDict ={}
#Check that we only have one parcel and get the attributes from it
if int(arcpy.GetCount_management(parcel_lyr).getOutput(0))==1:
for parrow in arcpy.da.SearchCursor(parcel_lyr,fldList):
for w in range(len(fldList)):
fldDict[fldList]=parrow
del parrow
targetFields = ['ACCOUNT', 'OwnerName', 'SiteAddres', 'SiteNum', 'siteNumSfx', 'Predir', 'Postdir', 'SiteCity', 'SiteZip',
'SiteState', 'FacltyType', 'StructType', 'GIS_STEW', 'UpdateBy', 'Verified', 'Status', 'StructCat', 'APA_CODE',
'AddressID', 'POINT_X', 'POINT_Y', 'StreetName','SHAPE@XY']
tprows = arcpy.da.InsertCursor(targetpoint, targetFields)
row = []
#Attributes from parcels
row.append(fldDict['ACCOUNT'])
row.append(fldDict['OwnerName'])
row.append(fldDict['SiteAddres'])
row.append(fldDict['SiteNum'])
row.append(fldDict['siteNumSfx'])
row.append(fldDict['Predir'])
row.append(fldDict['Postdir'])
row.append(fldDict['SiteCity'])
row.append(fldDict['SiteZip'])
#Preset attributes
row.append('ID')
row.append('Single Family Home')
row.append('Primary, Private')
row.append('CanyonCo')
row.append('TA')
row.append("Yes, GRM, TA")
row.append('Active')
row.append('Residential')
row.append('1110')
row.append(AddressID)
row.append(x)
row.append(y)
row.append(fldDict['SiteStreet'] + " " + fldDict['StreetType'])
row.append(point1)
tprows.insertRow(row)
arcpy.RefreshActiveView()
del tprows
#convert x,y to lat & long
latLonRef = "Coordinate Systems\Geographic Coordinate Systems\North America\NAD 1983.prj"
featureClasses = arcpy.GetParameterAsText(0)
featureClassesList = featureClasses.split(";")
for featureClass in featureClassesList:
rows = arcpy.UpdateCursor(targetpoint, "", latLonRef)
for row in rows:
feat = row.getValue("shape")
cent = feat.centroid
# To get the polygon area: cent = feat.area
row.POINT_X = cent.Y
row.POINT_Y = cent.X
rows.updateRow(row)
#arcpy.AddMessage(str(lat) + ", " + str(lon))
else:
arcpy.AddMessage("Number of parcels selected incorrect for this process") Message was edited by: 2CDSD 2C
Solved! Go to Solution.
That's because you're appending the variables 'x' and 'y' that you calculated at the beginning. If you want 'x' and 'y' to hold the lat/long values, you need to assign them as such.
x = pointA.centroid.X y = pointA.centroid.Y
...but now I'm not even sure what the 'point' feature set is supposed to be doing. Anyhow, I think your code has undergone so many revisions that you should really sit down and figure out what all your variables are doing.
my thought was to get the x,y and convert them to lat and long right when the point is created.
i made the change to your suggestion and it did change the x,y to lat & long but the coordinates are not right.
the point should be at x = -116.538104 and Y=43.723481
but instead it's at X= -125.292411 and Y= 41.269025
I was able to get to work with the following. I couldn't get to work correctly with the arcpy.PointGeometry.projectAs
my apologies as i am trying to learn python, thanks to all who replied.
#import modules
import arcpy
"""
tool box parameters
point param = feature set
targetpoint = featurelayer
parcel = featurelayer
"""
arcpy.env.qualifiedFieldNames = False
point = arcpy.GetParameterAsText(0) #point boolen
targetpoint = "CCAP2" #target point feature class
parcel = "parcels" #target feature class
parcel_lyr = 'parcel_lyr'
for prow in arcpy.da.SearchCursor(point,'SHAPE@XY'):
x,y = prow[0]
del prow
point1 = arcpy.Point(x, y)
ptGeometry = arcpy.PointGeometry(point1)
latLonRef = "Coordinate Systems\Geographic Coordinate Systems\North America\NAD 1983.prj"
featureClasses = point
featureClassesList = featureClasses.split(";")
for featureClass in featureClassesList:
rows = arcpy.UpdateCursor(point, "", latLonRef)
for row in rows:
feat = row.getValue("shape")
centroid = feat.centroid
x= centroid.X
y= centroid.Y
rows.updateRow(row)
CC_list = []
with arcpy.da.SearchCursor(targetpoint, ["AddressID"]) as cursor:
for row in cursor:
try:
if "CC" in row[0]:
CC_list.append(int(row[0].strip("CC")))
except TypeError:
pass
del cursor
CC_list.sort()
AddressID = CC_list[-1] + 1
AddressID = 'CC' + str(AddressID)
arcpy.MakeFeatureLayer_management(parcel,parcel_lyr)
arcpy.SelectLayerByLocation_management(parcel_lyr,"INTERSECT",ptGeometry)
fldList = ['ACCOUNT','OwnerName','SiteAddres','SiteNum','siteNumSfx','Predir','SiteStreet', 'StreetType', 'Postdir', 'SiteCity', 'SiteZip']
fldDict ={}
#Check that we only have one parcel and get the attributes from it
if int(arcpy.GetCount_management(parcel_lyr).getOutput(0))==1:
for parrow in arcpy.da.SearchCursor(parcel_lyr,fldList):
for w in range(len(fldList)):
fldDict[fldList]=parrow
del parrow
targetFields = ['ACCOUNT', 'OwnerName', 'SiteAddres', 'SiteNum', 'siteNumSfx', 'Predir', 'Postdir', 'SiteCity', 'SiteZip',
'SiteState', 'FacltyType', 'StructType', 'GIS_STEW', 'UpdateBy', 'Verified', 'Status', 'StructCat', 'APA_CODE',
'AddressID', 'POINT_X', 'POINT_Y', 'StreetName','SHAPE@XY']
tprows = arcpy.da.InsertCursor(targetpoint, targetFields)
row = []
#Attributes from parcels
row.append(fldDict['ACCOUNT'])
row.append(fldDict['OwnerName'])
row.append(fldDict['SiteAddres'])
row.append(fldDict['SiteNum'])
row.append(fldDict['siteNumSfx'])
row.append(fldDict['Predir'])
row.append(fldDict['Postdir'])
row.append(fldDict['SiteCity'])
row.append(fldDict['SiteZip'])
#Preset attributes
row.append('ID')
row.append('Single Family Home')
row.append('Primary, Private')
row.append('CanyonCo')
row.append('TA')
row.append("Yes, GRM, TA")
row.append('Active')
row.append('Residential')
row.append('1110')
row.append(AddressID)
row.append(x)
row.append(y)
row.append(fldDict['SiteStreet'] + " " + fldDict['StreetType'])
row.append(point1)
tprows.insertRow(row)
arcpy.RefreshActiveView()
del tprows
#convert x,y to lat & long
Here is an example using projectAs:
import arcpy # From WGS 1984 : (4326) x = -77.035286 y = 38.889469 # Into WGS 1984 Web Mercator (auxiliary sphere) : (3857) ptGeometry = arcpy.PointGeometry(arcpy.Point(x,y),arcpy.SpatialReference(4326)).projectAs(arcpy.SpatialReference(3857)) print ptGeometry.JSON print ptGeometry.firstPoint.X, ptGeometry.firstPoint.Y
Thank you Randy.