Francois Koutchouk
asked on
How do I search a shapefile using GPS coordinates?
I have a standard shapefile. It contains many shapes with their attributes.
I have a list of GPS coordinates that I know for sure are within the area of that shapefile.
How -- preferably with Python -- can I supply the lat/long and get an handle to that shape?
I have a list of GPS coordinates that I know for sure are within the area of that shapefile.
How -- preferably with Python -- can I supply the lat/long and get an handle to that shape?
ASKER
Thank you, I've tried 1 but cannot see how I could find the index of a shape (record) by supplying a GPS point (lat/long).
i never used this library, but i made a simple app in python, hope it gives you some idea:
code:
---------- ---------- --------
you can try it in the online compiler:
Link to Code
hope it helps
Istvan
code:
import shapefile
sf = shapefile.Reader("shapefiles/blockgroups.shp")
sf = shapefile.Reader("shapefiles/blockgroups.dbf")
#with shapefile.Reader("shapefiles/blockgroups.shp") as shp:
# print(shp)
shapes = sf.shapes()
pointindex=-1
shapeIndex=-1
for shape in shapes:
shapeIndex=shapeIndex+1
for point in shape.points:
pointindex=pointindex+1
#print('shape[idx:',shapeIndex,']= X(',point[0],'),Y(',point[1],')','\n')
if (point[0]==-122.465628 and point[1]==37.66665) or (point[0]==-122.469816 and point[1]==37.675509):
print('Found Coordinate ! Shape index: ', shapeIndex,', Point index: ',pointindex)
--------------------------you can try it in the online compiler:
Link to Code
hope it helps
Istvan
ASKER
Excellent! The code gives me all the points of the shape. Providing the lat/long of any of those points identifies the shape.
The question is how do I know if the lat/long is within the polygon defined by the lines connecting those points?
The question is how do I know if the lat/long is within the polygon defined by the lines connecting those points?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Fantastic answer, thank you very much.
you could try this python libraries:
1. pyshp
2. pyshapelib
3. shapelib
Hope it helps,
Istvan