Last active
August 22, 2022 16:44
-
-
Save jamesdamillington/8cf697bf2f0f64949acefe2b22409615 to your computer and use it in GitHub Desktop.
reduce the precision of geometries in a Pandas GeoDataFrame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#when saving GeoDataFrames as csv, high precision geometries can produce large files on disk | |
#reducing unnecessary precision can help reduce file size | |
#e.g. ONS digital vector boundary data converted from shp to gpd results in nano-metre precision! | |
import geopandas as gpd | |
from shapely.wkt import loads | |
from shapely.wkt import dumps | |
#set desired number of decimal places (e.g. see https://xkcd.com/2170/) | |
precision = 0 | |
simple = not_simple.copy(deep=False) #not_simple is a Pandas GeoDataFrame (with geometries in 'geometry' column) | |
#with help from https://gis.stackexchange.com/a/336382 | |
simple.geometry = not_simple.geometry.apply(lambda x: loads(dumps(x,rounding_precision=precision))) | |
#to remove dps entirely (truncate), run next line after last line using precsion=0 | |
#e.g. useful when working with British National Grid CRS (units metres) | |
trunc.geometry = simple.geometry.apply(lambda x: loads(dumps(x,trim=True))) | |
#then write | |
trunc.to_csv(not_simple_truncated.csv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment