Search code examples
shapefilepysal

How to use Shapefile in Pysal


I want result of the points intersecting in the given polygon, but I am getting an error.

My code is:

from pysal.cg.standalone import get_polygon_point_intersect
poly=pysal.open('Busroute_buffer.shp')
point=pysal.open('pmpml_24.shp')

i=get_polygon_point_intersect(poly,point)

But I am getting the error message:

'PurePyShpWrapper' object has no attribute 'bounding_box'


Solution

  • pysal.open returns a shape "file" object, not shapes.

    To get the shapes out you need to iterate over the file, or call the file's read method, which returns a list of shapes. This will return a list even if there is only 1 shape in your file. get_polygon_point_intersect takes exactly 1 polygon and 1 point, so you'd need to call it for each point/polygon you want to compare.

    point_file = pysal.open('points.shp')
    polygon_file = pysal.open('polygons.shp')
    # .read with no arguments returns a list of all shapes in the file.
    polygons = polygon_file.read()
    for polygon in polygons:
        # for x in shapefile: iterates over each shape in the file.
        for point in point_file:
            if get_polygon_point_intersect(polygon, point):
                print point, 'intersects with', polygon
    

    There are other perhaps more efficient ways of doing this as well. See the pysal.cg.locators for more information.

    *The above code is untested and for example purposes only.