1

Geopandas

Reading a shapefile with GeoPandas and printing its total bounds:

dataframe = gpd.read_file('example.shp')
print(dataframe.total_bounds)

Prints: [ 663590.5817 1541419.8307 724630.0589 1595869.4839]

PyQGIS

Doing the same with PyQGIS:

QgsApplication.setPrefixPath("/usr", True)
qgs = QgsApplication([], False)
qgs.initQgis()
layer = QgsVectorLayer('example.shp', 'layer1', 'ogr')
print(layer.extent())

Prints: <QgsRectangle: 663590.58169999998062849 1541419.83070000074803829, 724630.05890000029467046 1595869.48389999940991402>


As you can see doing it with PyQGIS gives you more precision than doing it with GeoPandas.

Is there any way to get the same precision with GeoPandas?

gene
  • 54,868
  • 3
  • 110
  • 187
David1212k
  • 355
  • 1
  • 11
  • 4
    If you aren't mapping Higgs Boson detection in UTM, then it seems unlikely that a tenth millimeter is an appropriate precision much less tens of femtometers. You'd probably be better off rounding to meters. Obligatory XKCD reference: https://xkcd.com/2170/ – Vince Sep 15 '19 at 03:31
  • Yes, definitely don't need that much precision, thanks for the links – David1212k Sep 15 '19 at 10:44
  • What does print(dataframe.total_bounds[0]) print? – inc42 Sep 15 '19 at 11:30
  • It prints: 663590.5817 – David1212k Sep 15 '19 at 11:38
  • 1
    Next to not needing such high precision in most cases, it is probably also just a "representation" issue: pandas and numpy will by default only show the first decimals when printing them. – joris Sep 15 '19 at 14:43

2 Answers2

2

Use geopandas.options.display_precision to specify how many decimal places to be visualized when printing the dataframe.

Note that internally the precision is not rounded, this is just a matter if displaying the coordinates.

Example, default behaviour:

>>> import geopandas
>>> world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
>>> world['centroid_column'].head()
0    POINT (163.85316 -17.31631)
1      POINT (34.75299 -6.25773)
2     POINT (-12.13783 24.29117)
3     POINT (-98.14238 61.46908)
4    POINT (-112.59944 45.70563)
Name: centroid_column, dtype: geometry

With 9 decimal places now:

>>> geopandas.options.display_precision = 9
>>>
>>> world['centroid_column'].head()
0    POINT (163.853164645 -17.316309426)
1      POINT (34.752989855 -6.257732429)
2     POINT (-12.137831112 24.291172960)
3     POINT (-98.142381372 61.469076145)
4    POINT (-112.599435912 45.705628002)
Name: centroid_column, dtype: geometry

Taken from geopandas official documentation.

Campa
  • 785
  • 2
  • 8
  • 20
1

Try using pandas (pd) settings: pd.set_option('max_colwidth', value)

look here for more info: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.set_option.html

Ofir
  • 677
  • 8
  • 20