martinfleis / efficient-geopandas-workshop Goto Github PK
View Code? Open in Web Editor NEWWorkshop materials for the Writing an efficient code for GeoPandas and Shapely in 2023
License: BSD 3-Clause "New" or "Revised" License
Workshop materials for the Writing an efficient code for GeoPandas and Shapely in 2023
License: BSD 3-Clause "New" or "Revised" License
Writing up here, and with some questions (can push when I am sure you are not doing that to avoid conflicts in the notebook)
QUESTION: How to calculate the distance between all points of two GeoDataFrames?
points1 = geopandas.points_from_xy(numpy.random.uniform(0, 1000, 1_000), numpy.random.uniform(0, 1000, 1_000))
gdf1 = geopandas.GeoDataFrame(geometry=points1)
points2 = geopandas.points_from_xy(numpy.random.uniform(0, 1000, 1_000), numpy.random.uniform(0, 1000, 1_000))
gdf2 = geopandas.GeoDataFrame(geometry=points2)
(can maybe just use one dataframe and do pairwise with itself to simplify)
%%time
# do not use this code
gdf1.geometry.apply(lambda g: gdf2.distance(g))
-> ca 750ms
We can use the broadcasting of the shapely ufunc by reshaping as 2D arrays (a (N, 1) and (1, N) gives (N, N) result):
%%time
shapely.distance(np.asarray(gdf1.geometry)[:, np.newaxis], np.asarray(gdf2.geometry)[np.newaxis, :])
-> ca 500ms
Note: so this doesn't give that much of a speedup nowadays, since both are using the fast ufunc under the hood, and distance is a relatively expensive function, and so you are only avoiding the inner loop for one dimension.
So it's not that big of an improvement.
I was think that we could show as a follow-up to use an strtree: what if you only want the distances for points that are less than xx m away from each other?
tree = shapely.STRtree(gdf1.geometry)
%%time
idx2, idx1 = tree.query(gdf2.geometry, predicate="dwithin", distance=100)
shapely.distance(np.asarray(gdf1.geometry)[idx1], np.asarray(gdf2.geometry)[idx2])
-> ca 80ms
We can't directly use query_nearest
here, unless you of course are fine with just getting the one nearest within a search radius, instead of all within a search radius.
I suppose we will already show nearest as well in another example, or can combine it with this one?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.