I am trying to join points to polygons based on whether their addresses match as well as if the distance between them is less than 2500 feet. I defined a haversine function that computes the distance between the point and the polygon using their coordinates. However I get an error that the sql is invalid.
import arcpy import harversine arcpy.env.overwriteOutput=True arcpy.env.workspace=r"C:\mygdb.gdb" where="pointshp.CAdd = polyshp.P_Address and harversine.myhaversine(pointshp.NewLong,pointshp.NewLat,polyshp.polyX,polyshp.polyY) < 2500" keyfield="pointshp.OBJECTID" arcpy.MakeQueryTable_management(["pointshp","polyshp"],"joined","USE_KEY_FIELDS",keyfield,"",where) arcpy.CopyFeatures_management("joined","joined")
The error I get:
An invalid SQL statement was used. [joined]
haversine function I use which is saved in a separate script called
harversine.py i.e. it is not a spelling mistake.
import math def myhaversine(lon1, lat1, lon2, lat2): # convert decimal degrees to radians lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2]) # haversine formula dlon = lon2 - lon1 dlat = lat2 - lat1 a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2 c = 2 * math.asin(math.sqrt(a)) r = 20887680 # Radius of earth in kilometers. Use 3956 for miles, 20,887,680 feet return math.ceil(c * r)