Integration How To

55 views
Skip to first unread message

Jim St

unread,
Aug 16, 2011, 10:24:54 AM8/16/11
to Geospatial Python
Hello-

Given:
- A shapefile containing one or more polygons
- A list of lat\lon pairs

What is the most efficient way to iterate through the polygons in the
shapefile to determine which one the point falls within? None of the
polygons overlap each other so I would expect to match at most to a
single polygon. The approach I'm pondering is:

Read shapefile
for each shape read points into list
iterate through list (perhaps using bounding box as a crude filter)
using Ray Casting approach to determine if point falls within polygon.

On a vaguely related note, how do I associate given shape (shape[n])
with its 'Id'?

Thank you.

Jim

Geospatial Python

unread,
Aug 16, 2011, 12:47:15 PM8/16/11
to Geospatial Python
Jim,

Your algorithm is spot on and I would definitely filter with the
bounding box first. Beyond that you would have to get into spatial
indexing <http://www.pygame.org/wiki/QuadTree> which is sufficiently
complex enough that I haven't played with it yet in pure Python.

As for you ID question... The FIDs are just an illusion by ArcGIS
(and other packages) for convenience. The shapefile format doesn't
support any sort of id or numbering. The indexes (i.e. shape[n]) are
implied by the order they appear in the shapefile. When you open a
shapefile in ArcGIS and open the attribute records, the FIDs are not
persistent. If you delete a record, say #7, the attribute table will
jump from 6 to 8. But if you close that shapefile and reopen it in
ArcGIS, former record #8 will now be #7 the same as if you delete a
row in Excel. The ids are not persistent. If you are using ArcSDE or
PostGIS the database may insert persistent object ids but that is a
feature of the database and not the data format.

At NVision <nvs-inc.com> we will often add our own permanent ID column
to a shapefile and autonumber it so that the ID will be persistent if
you need unique tags for records.

Good luck and let us know if we can be of further assistance.

- Joel
Reply all
Reply to author
Forward
0 new messages