question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Functions to Clip Point, Line and Polygon Data Like Those that exist in other spatial GIS tools

See original GitHub issue

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:

  • Clip takes a point, line or polygon feature to be clipped and a boundary polygon as the clipping extent
  • Clip outputs a point, line or polygon feature that has ONLY features contained in the boundary polygon. Each feature if it’s a line or poly is thus “CLIPPED” to that extent if that is required.
  • Clip maintains the attributes of the clipped point, line or polygon features

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 the how="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

# Create function to clip point data using geopandas


def clip_points(shp, clip_obj):
    '''
    Docs Here
    '''

    poly = clip_obj.geometry.unary_union
    return(shp[shp.geometry.intersects(poly)])

# Create function to clip line and polygon data using geopandas


def clip_line_poly(shp, clip_obj):
    '''
    docs
    '''

    # Create a single polygon object for clipping
    poly = clip_obj.geometry.unary_union
    spatial_index = shp.sindex

    # Create a box for the initial intersection
    bbox = poly.bounds
    # Get a list of id's for each road line that overlaps the bounding box and subset the data to just those lines
    sidx = list(spatial_index.intersection(bbox))
    shp_sub = shp[shp.index.isin(sidx)]

    # Clip the data - with these data
    ne_roads_sub['geometry'] = ne_roads_sub.intersection(poly)

    # Return the clipped layer with no null geometry values
    return(ne_roads_sub.geometry.notnull())

## I just updated the above as I realized I could do this with one less line by updating the geometry col

# Final clip function that handles points, lines and polygons


def clip_shp(shp, clip_obj):
    '''
    '''
    if shp["geometry"][0].type == "Point":
        return(clip_points(shp, clip_obj))
    else:
        return(clip_line_poly(shp, clip_obj))

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:35 (25 by maintainers)

github_iconTop GitHub Comments

1reaction
jorisvandenbosschecommented, Aug 20, 2019

I think there are two separate issues here:

  • overlay can definitely be improved to work with other geometries than polygons. See also the last bullet point in https://github.com/geopandas/geopandas/issues/706, and I seem to remember we were recently talking about this (maybe on gitter?).

  • Apart from overlay, I think it is still valuable to have a separate clip function. It is conceptually simpler as a full overlay (you “just” want to clip the left geodataframe by the right polygon, and not “combine” it with the right geodataframe). See also https://github.com/geopandas/geopandas/issues/956 and https://github.com/geopandas/geopandas/issues/921. @lwasser I think the code (and example) you show above from earthpy looks really good (that is also more or less how I would implement a clip function). Would you like to contribute this clip module to geopandas?

1reaction
martinfleiscommented, Aug 19, 2019

I will reopen this issue as it is something I feel geopandas should support. Preferably as an enhancement of overlay to work on all types of geometries.

Read more comments on GitHub >

github_iconTop Results From Across the Web

7 Geoprocessing Tools Every GIS Analyst Should Know
To clip data, you need points, lines, or polygons as input and a polygon as the clipping extent. The preserved data is the...
Read more >
Clip features using other features—ArcGIS Pro | Documentation
The Clip tool clips visible and editable features where they intersect selected features at a specified buffer distance. You can clip all editable...
Read more >
How to Clip Point and Polygon Features in ArcGIS Pro |
The Clip tool extracts input features that overlay the clip features. The following examples use two polygon layers of geographical boundaries ...
Read more >
Chapter 7: Geospatial Analysis I: Vector Operations
geoprocessing operation is used to extract those features from an input point, line, or polygon layer that falls within the spatial extent of...
Read more >
GIS dictionary - Esri Support
The data structure in a coverage used to represent linear features and polygon boundaries and to support analysis functions, such as network tracing....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found