-
Notifications
You must be signed in to change notification settings - Fork 927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Functions to Clip Point, Line and Polygon Data Like Those that exist in other spatial GIS tools #821
Comments
@lwasser I've not run the above code, so maybe I'm missing something, but it isn't clear where this differs from performing an |
Hi @jdmcbr I added some clarification above as well. I should have mentioned this too. I spent some time looking at overlay. It appears however that overlay only supports intersecting polygons. And yet i want to be able to do this with points and lines as well and for the implementation (on the user end) of a "clip" operation to be uniform. Please let me know if I missed something! If i implemented overlay incorrectly please do let me know. I looked through the docs and code and it seems it wants polygons only. |
Hey @lwasser |
hey @waldnerf i ended up including the code to clip here in https://github.com/earthlab/earthpy/blob/master/earthpy/clip.py it handles points, lines and polygons and should handle multi features too. |
I will reopen this issue as it is something I feel geopandas should support. Preferably as an enhancement of |
that would be great @martinfleis !! if you guys implement it please let me know so i can remove it from earthpy !! then i don't have to support it! i put it over there as we needed it in our classes and it hadn't been addressed. feel free to have a look at the earthpy code. we tested out a few different approaches and found that an rtree approach was the fastest ... however you may have other approaches. clip slows down a lot with large datasets and would probably be optimal in parallel if that were available. the biggest issue coming from a GIS / User perspective that i see if that overlay only works on polygons. And yet most GIS type users (people working with spatial data) will want to clip not only polygons but also lines and points. Normally in gis tools clip is not specific to the geometry type - it works on any geometry type. that is what a GIS user might expect. from a developer perspective however, clip does have to be managed different for lines and points sometimes.. and then ofcourse multi features... |
@lwasser check this branch https://github.com/martinfleis/geopandas/tree/overlay_geoms I'll play with it a bit more to figure what could be the issue. You are welcome to do so as well. |
I think there are two separate issues here:
|
I remember that. It does makes sense to extend its functionality. I have found some corner cases these changes bring, I'll open a PR so we can discuss that in relation to the actual code.
You are right about having |
The problem with the overlay one is that it will also split your original geometries in the left dataframe if they are crossed by an internal border of the right dataframe (so not just by the extent). Which is something we don't want for the clip behaviour (at least, that is how I currently imagine the clip behaviour: it's only clipping the original geometries by the full extent of the other geometry/geodataframe), so we would need to do a dissolve again after the But the performance issue is interesting to check to see how it compares (the earthpy variant should also benefit the optimized vectorized operations. And in general we should try to include spatial index in those default operations as well, but that's another issue :)). |
Hmm, you are right. Dissolve is a way, but it changes original geometry to multi geometry. This snippet is adapted
|
Also, I think for an initial version figuring out the desired functionality is more important (and for that the earthpy's code seems good to reason about), and we can still profile / optimize in a second step. |
Trying earthpy, it seems that the desired functionality is the one @lwasser implemented, not the one above. Geometry should be kept intact if it's fully within clipping polygon. |
@jorisvandenbossche i'd be so happy to contribute our clip function to geopandas! i spent a lot of time optimizing it and we just recently added support for multi part features. i can also provide tests and a vignette. How would you like for me to submit this? PR with the code and tests and vignette? and then i do think a future implementation would be to support parallel processing as it is still slow with big datasets BUT i did test multiple approaches the one in the code is the fastest. this is awesome! also want to ping @mbjoseph and @nkorinek who worked on the modules as well :) |
Yes, a PR with all three of code, tests and docs is perfect! |
wonderful. @jorisvandenbossche i'll have a PR to you in the next few days and will ping you when it's submitted. i have a few things to work on and a lot of meetings this week but i'll get it to you! more soon! |
awesome @jorisvandenbossche et al... can you please tell me what the review process is here for my pr? i'm still working on it although all tests are passing now so i'm getting close. Should i just respond to all comments? Or should I wait because other maintainers may have other comments? Thank you so much!! |
I'm trying both solutions above. My polygon are composed several adjacent polygons. I want to preserve the geometry that intersects with each polygon. For example, in a line intersects with two polys, 2 linestring will be created. Is there a solution that avoids dissolving the polygon before clipping? |
@maning If I understand it correctly, I think you are looking for the |
@jorisvandenbossche but the |
Sorry, missed that part of your description. There is an open PR that adds that functionality to |
Wow! I tested the line with poly intersection and it works! The output is the same as what I get in QGIS on a small area. Thanks! |
@maning Thanks for testing! |
Thank you for this useful tool. Am currently stuck at getting a return GeoDataFrame without the attributes from the clip_obj data frame. Can someone help out??? I want the information from the clip_obj not just its geometry. <<<<#The returned GeoDataFrame is a clipped subset of shp that intersects with clip_obj.# <<<<<<----- This here is the problem. Can I get a Geodataframe with attributes from the clip_obj as well? |
@DchemistRae Then you are looking for overlay, not clip. |
Thank you for the insight. However, overlay cannot be applied on linestrings. And using buffer messes up my geometry |
@DchemistRae There is a PR #1110 allowing all geometry types in overlay, you can try that and let us know if it works as expected. |
Please how do I use this new feature? I updated the pandas package. But I still got the same multipolygon error. |
@DchemistRae You'll have to install dev version of GeoPandas from that specific branch.
|
How can i install since I use conda? And I suppose I will have to install Git and set path? |
You will have to install git, |
All installed, overlay gave an attribute error. Both GeoDataFrames have geometry type Linestrings. AttributeError: module 'pandas.api.indexers' has no attribute 'check_bool_array_indexer' |
@DchemistRae This is not the right place for this discussion, but you cannot clip one Linestring layer with another Linestring layer, that does not make geometrically much sense as you expect area for clip. Can you report that AttributeError with a few more details (pandas version, actual code used, sample data) in #1110? |
yay closed via #1128 !!! :) 🎆 |
Hi geopandas dev team:
The Issue / Question That I have
I am trying to come up with a consistent way to "clip" data that is in line with a typical GIS (think arcgis or qgis) workflow as follows:
On the back end - clipping points, lines or polygons is actually quite different. On the front end however - to a user - it is the "same" input and output.
input: some (shapefile) i want to clip to an extent
output: some clipped shapefile
What I tried initially -
overlay()
Initially i tried to use
overlay()
with thehow="intersection"
argument. however that approach only seems to support polygon features. Did I do something wrong? I spent some time on this.What I did / Tried
I am trying to create a simpler way for my students to "clip" a point, line or polygon layer using a boundary polygon object that exposes less of the back end differences between these object types when processed.
Below i've created a set of functions to make up a parent function that clips points, lines or polygons. I wondered if you could have a look at my code to help me determine if my approach is the most efficient one. I haven't built any tests -- yet but will.
Question 2 -- If applicable would this fit in geopandas? Or is there a better approach?
My next question if you don't mind and if i am able to find an approach that fits the project -- is if the geopandas project is open to such a function housed in geopandas? It is a task I and my colleagues do often!
Any feedback is appreciated. I do still owe you another PR for more documentation surrounding dissolve as well.
The Functions I Wrote
The text was updated successfully, but these errors were encountered: