I have a shapefile, which I converted into geopandas dataframe. I would like to round the coordinates to 5 decimals.

Is there a method/function in geopandas that can do this?

My shapefile contains multipolygons.

  • 2
    Be careful. Rounding of coordinates can damage polygon topology. Rounding degrees to 5 places could change coordinate placement by 1 meter. – Vince May 2 '19 at 18:08
  • I saw the precision table and decided 1 meter is good enough for my application. Hence 5 decimals. – Vinay May 2 '19 at 18:09
  • Except it doesn't work like that. If the data has centimeter resolution, coordinates may come closer than a meter, and the resulting truncation could cause polygon rings to touch. It takes a more sophisticated approach than just rounding coordinates. – Vince May 2 '19 at 18:18
  • But why? What do you gain from this? The object will still occupy the same size in memory, you might snap nodes together and break topology, you'll modify the area and perimeter of your features. Maybe you can do this validly for point features, but I'd never do this for lines or polygons, unless its for display purposes only (ie showing a table of vertex coordinates). – Spacedman May 2 '19 at 18:18
  • 1
    We are developing an interactive d3 visualization tool which shows all the US zips on a map with other features. Interaction speed is important in this case and I thought decreasing the precision will help in loading and displaying data fast. Is there any validity in my argument? @Vince - I really don't care if they overlap little bit. – Vinay May 2 '19 at 18:24

You can use the regex module to find the coordinates in a wkt representation of the geometries, round and load back:

import geopandas as gpd
from shapely.wkt import loads
import re

simpledec = re.compile(r"\d*\.\d+")
def mround(match):
    return "{:.5f}".format(float(match.group()))

shapefile = '/home/bera/GIS/data/test/polys.shp'
df = gpd.read_file(shapefile)
df.geometry = df.geometry.apply(lambda x: loads(re.sub(simpledec, mround, x.wkt)))

See: Rounding using regular expressions

enter image description here

Or try this: Is it possible to round all coordinates in shapely?

| improve this answer | |
  • The second method doesn't work if there are multi-polygons in geometry. Although we can identify them and loop it differently. – Vinay May 2 '19 at 19:49
  • Ok! To bad a function like postgis ST_SnapToGrid does not seem to exist for shapely. Im sure the comments are correct that simply rounding the coordiantes can mess up the geometries. – BERA May 2 '19 at 19:52

I recommend researching topojson and mapshaper as these tools were created to intelligently simplify shapes, preserving topology. Both tools are written in javascript. Mapshaper has a precision option for the output. You can run Mapshaper through a website, mapshaper.org or download the command line tools.

| improve this answer | |
  • Can't upvote due to reputation but it works. – Vinay May 2 '19 at 19:01
  • Mapshaper is removing other properties in my shapefile and retaining only geometry. Is there a command to include properties? I remember this was not the case when I reduced a shapefile 6 months ago. – Vinay May 2 '19 at 19:26
  • This might help with the attributes, gis.stackexchange.com/questions/238204/… – klewis May 2 '19 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.