Link to home
Start Free TrialLog in
Avatar of Francois Koutchouk
Francois KoutchoukFlag for United States of America

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?
Avatar of Molnár István
Molnár István
Flag of Romania image

Hi,

you could try this python libraries:
1.  pyshp
2.  pyshapelib
3.  shapelib

Hope it helps,
Istvan
Avatar of Francois Koutchouk

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:
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)

Open in new window

------------------------------------------------------
you can try it in the online compiler:
Link to Code

hope it helps
Istvan
the shape file in google maps looks like this:

Link to Google Maps
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?
ASKER CERTIFIED SOLUTION
Avatar of Molnár István
Molnár István
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fantastic answer, thank you very much.