pysal / libpysal

Core components of Python Spatial Analysis Library
http://pysal.org/libpysal
Other
265 stars 78 forks source link

on-the-fly W #304

Open darribas opened 10 years ago

darribas commented 10 years ago

This is originally a question, but if not implemented, it could be a nice additional user method to have. Is it possible to build a W from a subset of polygons from a shapefile?

Test-case example: calculate W for counties in only one state using a shapefile of all counties in the US. What I have in mind is something like ps.queen_from_shapefile but, instead of taking a path to a shapefile, it takes an iterable of polygons.

This would be useful for my example case, but also in many more contexts. Take the hypothetical case in which a user extracts a subset of polygons from a large database (e.g. spatialite) into memory and needs to build a W matrix from there (assuming the user has a way of converting the polygons from spatialite to PySAL geometries).

lanselin commented 10 years ago

Actually, this is a great idea and fits with some things Serge and I have been talking about. I had a similar use case in the context of an unbalanced spatial panel, where I basically had to create subsets of the shape file to get the weights for each time period and then merge them. So, merging weights files would be useful too. L.

Luc Anselin, PhD Visiting Professor Department of Urban Studies and Planning MIT

Regents' Professor and Walter Isard Chair Director, GeoDa Center for Geospatial Analysis and Computation School of Geographical Sciences and Urban Planning Arizona State University

Editor, International Regional Science Review

On Thursday, November 28, 2013 at 5:04 AM, Dani Arribas-Bel wrote:

This is originally a question, but if not implemented, it could be a nice additional user method to have. Is it possible to build a W from a subset of polygons from a shapefile?
Test-case example: calculate W for counties in only one state using a shapefile of all counties in the US. What I have in mind is something like ps.queen_from_shapefile but, instead of taking a path to a shapefile, it takes an iterable of polygons. This would be useful for my example case, but also in many more contexts. Take the hypothetical case in which a user extracts a subset of polygons from a large database (e.g. spatialite) into memory and needs to build a W matrix from there (assuming the user has a way of converting the polygons from spatialite to PySAL geometries).

— Reply to this email directly or view it on GitHub (https://github.com/pysal/pysal/issues/378).

ljwolf commented 7 years ago

would this be accomplished by the new .from_dataframe & from_iterable methods on the weights classes now?

You could create a lazy filter iterator over a shapefile and construct it from that in a single read:

handle = pysal.open('file.shp')
filtered = (shp for shp in handle if condition)
ps.weights.Queen.from_iterable(filtered)
darribas commented 7 years ago

Yes, I'd think so. The geotable and the from_dataframe would let you do that!