Select to view content in your preferred language

Is it possible to do a sql query of a feature layer using geometry fields?

451
1
11-01-2023 08:04 AM
ColeHowell
Occasional Contributor

I'm working on a script that will take data about water meters from our recording software and put the necessary data into the GIS. Basically things like flow and the gps location. Basically, I want to see if a point has already been added to the map at the location recorded. Can I do a sql query based on latitude and longitude? How might I do that? I'm not very familiar with sql itself.

Tags (3)
0 Kudos
1 Reply
Clubdebambos
Frequent Contributor

Hi @ColeHowell 

This is possible with the query() method for an Feature Layer object.

Workflow:

  • Create a Point object based on long/lat
  • Access your feature layer
  • query feature layer using geometry filter: need to apply a small buffer to each point in the feature layer.

You will need to tinker with the optimum distance to buffer each point and the number of decimal places for each geometry to finesse to your needs. You might also need to account for alternate Spatial References.

But this should help you out.

from arcgis.gis import GIS
from arcgis.geometry import Point
from arcgis.geometry.filters import intersects

## access AGOL
agol = GIS("home")

################################################################################
## set the longitude and latitude you want to search for

longitude = -9.025675
latitude = 53.283417

################################################################################
## create a point geoemtry based on the longiture and latitude supplied

pt2check = Point({"x" : longitude, "y" : latitude, "spatialReference" : {"wkid" : 4326}})

################################################################################
## access the feature service and the feature layer of interest

item = agol.content.get("FS_ITEM_ID")

# use the correct index to access the layer
fl = item.layers[0]

################################################################################
## query the layer based on an intersection of the pt2check with a small
## buffer around each point

query = fl.query(
    distance=2, # dictance to buffer each point in the dataset
    units="esriSRUnit_Meter", # unites for the distance
    out_sr=4326, # srs to forece returned geometry to
    geometry_precision=6, # decimal places for returned geometry
    geometry_filter=intersects(pt2check), # filter based on intersection with distance buffer
    return_ids_only=True # return FIDs only
)

################################################################################
## print to screen. If an empty list then no intersection

print(query["objectIds"])

################################################################################
print("\nSCRIPT COMPLETE")

 

~ learn.finaldraftmapping.com
0 Kudos