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?

5
  • 5
    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: xkcd.com/2170
    – Vince
    Commented Sep 15, 2019 at 3:31
  • Yes, definitely don't need that much precision, thanks for the links
    – David1212k
    Commented Sep 15, 2019 at 10:44
  • What does print(dataframe.total_bounds[0]) print?
    – inc42
    Commented Sep 15, 2019 at 11:30
  • It prints: 663590.5817
    – David1212k
    Commented Sep 15, 2019 at 11:38
  • 2
    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
    Commented Sep 15, 2019 at 14:43

3 Answers 3

3

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.

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

0

You can change the data type in your GDF schema. The fiona documentation describes that you can specify a data type in the schema with its length for most data types, and you can also specify the precision of a float dtype. If you change the types in the schema, the accuracy and length will be preserved when importing to a file of the desired format, for example, ESRI ShapeFile.There are examples in the fiona documentation scheme section, the type is indicated (for example, float, followed by : and length, and then put . and the accuracy is indicated. Example: 'float:32.5'. For a given number, the length of the integer part will be 31 characters (a comma is considered a symbol, add 1 to get 32 digits before the comma), and its precision after the comma will be five.

An answer with an example of Python code: https://gis.stackexchange.com/a/483104/243816

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jun 26 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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