Thursday, 28 April 2016

Clipping shapefile with Python?


My problem is the following, I cannot find a reasonable method to clip 2 vector based shapefiles using python (without ArcPy or QGIS API).


I tried using the geometry.intersections method, but that would return a lake that should have been mostly clipped away (~2% of the lakes surface should stay after clipping with a boundary), so I figured the intersection method does not return what I want.


What I want to do is to import .shp files from my drive which I achieved using geopandas:


import matplotlib.pyplot as plt
import matplotlib.lines as mlines

from matplotlib.colors import ListedColormap
import geopandas as gpd
import os


boundary = gpd.read_file(boundary_within_features_of_the_other_layers_should_stay)

water = gpd.read_file(water_layer)

water_clipped = water[water.geometry.intersects(boundary)]


so this method didn´t work as I wanted. I want to clip more features, but I cant figure out how to do it or which library to use.


I also tried:


import os

wd = r"C:\Users\blablabla"

list_of_files = os.listdir(wd)

file_that_clips = r'C:\Users\blablabla.shp'


for file_to_clip in list_of_files:
if file_to_clip[-3:] == "shp": # If file is a shapefile
clipped_file = file_to_clip[8:-4] + "_clipped.shp" # New name for output
os.system("ogr2ogr -clipsrc " + file_that_clips + " " + clipped_file + " " + file_to_clip)
print(clipped_file + 'got clipped')

Which should have worked according to the last print statement, but the clipped layers couldn´t be found anywhere. So this doesn´t seem to work for me as well.




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...