# QGIS Polygon precision

Is there a way to edit the precision of polygon points in QGIS so that each coordinate has the same number of decimal places?

For example, the coordinate `23.18273917, -84.29172638491` would be converted to `23.18274, -84.29173` if `5` was entered.

• The number of sig figs cannot change, because the coordinates are represented in binary, not decimal. Are you perhaps asking how to round the coordinates? Because that will shift the vertices, could you explain why you might want to do this? – whuber Nov 8 '13 at 22:40
• Yes, rounding is certainly acceptable. I will be using the polygon points for use in other software that is only precise to 6 decimal points so the 17 QGIS gives is nice but in my case, unnecessary. Moreover, for any post-processing that I will end up doing on polygons, I will need to know the length in characters of each coordinate for use in PHP substring calls. – Brandon Schmidt Nov 8 '13 at 23:08
• I trust you are aware of the consequences of reducing precision. In this case, six sig figs could result in just four decimal places in the longitude, which rounds to around 10 meters. If that might be a problem, consider first translating the coordinates to be near (0,0), which will gain one or two more decimal places of precision--good enough for most GIS work--and then translating them back (if necessary) in the target software. – whuber Nov 8 '13 at 23:13
• Actually, what I meant to say was decimal places not significant figures...not sure why I mixed that up. Anyways though, I am unable to do any modifying of points with the second program; it simply attempts to draw a text file of points on a map. In addition, I could probably use 7 or 8 decimal places to create lossless rounding (at least perceptible in the second program) but 17 is too many without a doubt and besides, it needs to be consistent with all points. – Brandon Schmidt Nov 8 '13 at 23:22
• Do you want to round the coordinates of the vertices of a loaded shapefile? Or are you entering data in, for example, a dialogue such as the WKT plugin? – Simbamangu Nov 9 '13 at 8:32

Not a real 'rounding' solution, but you could use regular expressions to trim the decimal places in QGIS.

Use the Field Calculator and add a new text field (e.g. 'geom1') with the WKT of your whole polygon:

``````geomToWKT ( \$geometry )
``````

This will give you all the points of your polygons in WKT format, e.g. `POLYGON((34.77736006488765241 -7.4507487326605899,35.30901941128683319 -7.44604378269245526,35.59131640937489749 -7.71422593087611386,35.32783921115937176 -8.14237637797634051,34.64562146577988955 -8.02475262877298157,34.77736006488765241 -7.4507487326605899))`

Then, add and calculate a new text field (geom2) based on the WKT field:

``````regexp_replace ( "geom1", '(\\d+.\\d{6})\\d+', '\\1')
``````

This results in a new WKT field with trimmed (not rounded!) decimal places: `POLYGON((34.777360 -7.450748,35.309019 -7.446043, ...`

The regular expression finds numbers in the format `##.######...` (any number of decimals) and replaces them with `##.######` (exactly six decimals, change this by modifying the number inside the curly brackets `{}`).

Save the layer as a CSV file and then add it back to QGIS with the Add Delimited Text Layer, selecting the new geometry field:

Caution: depending on the size of your polygons, you may run into limits of the length of a text string in a shapefile's DBF field. Probably best to work within a spatialite format and use a varchar field for the new geometries.

• This looks pretty promising...I'll back up my file first and try this as soon as I can. If I run into any problems I'll let you know but if it works I'll be absolutely ecstatic. Thanks for such a detailed response! – Brandon Schmidt Nov 12 '13 at 19:41
• This might be a stupid question but what should I enter for the OGR creation options? If I enter nothing I get errors. – Brandon Schmidt Nov 12 '13 at 21:39
• You mean in the save-as dialogue? I leave them blank; just select "Comma separated value" in 'Format' and you should be good to go. What errors do you see? – Simbamangu Nov 13 '13 at 11:01
• "Feature creation error (OGR error: Failed to create .\final1.csv: Permission denied) Feature creation error (OGR error: Failed to create .\final1.csv: Permission denied) Feature creation error (OGR error: Failed to create .\final1.csv: Permission denied) Feature creation error (OGR error: Failed to create .\final1.csv: Permission denied)Stopping after 1001 errors" – Brandon Schmidt Nov 13 '13 at 15:21
• I get 1001 of these errors apparently and then the stop error. Could it be the size of my placefile causing the issue? I have about 3200 polygon features and each polygon contains anywhere from 4 all the way up to around 300 points. – Brandon Schmidt Nov 13 '13 at 15:23

If your geometry features are stored in a PostGIS database, you can use ST_SnapToGrid to "snap" the coordinates to the nearest 0.00001 degree grid. E.g.:

``````SELECT ST_AsText(ST_SnapToGrid('POINT(23.18273917 -84.29172638491)', 0.00001));
st_astext
---------------------------
POINT(23.18274 -84.29173)
(1 row)
``````

This works for any geometry, including polygons.

• Much nicer solution! The `ST_SnapToGrid()` function exists in Spatialite, too – so OP could save his layer in Spatialite then run a query in the DBManager and never 'leave' QGIS. – Simbamangu Nov 13 '13 at 15:43

The OpenJump desktop GIS has a tool called Precision Reducer. It is in the Tools\Edit Geometry menu. You must make your Shapefile editable (right-click layer, editable) to enable this tool. You can define the precision to preserve. You may want to first make a copy of your data.

• After trying this, I end up with points like this: 32.70738982999999678, -86.41278076000000397 32.70585632000000231, -86.71415709999999422 – Brandon Schmidt Nov 9 '13 at 0:18
• These are rounded to 8 decimal points but what's up with the trailing digits after that? Is that supposed to happen? – Brandon Schmidt Nov 9 '13 at 0:19

A quick-and-dirty way to do it would be to use the copy-and-paste option on the geometry in QGIS, then parse the resulting WKT in Excel or OpenOffice or equivalent. This is not too difficult with points, but nightmarish with anything more complex!

Select the geometry in QGIS:

Edit|Copy, then move to a blank spreadsheet and paste:

Parse using the MID() function to grab the X and Y coords, e.g. for this case `1*MID(A2,7,8)`, but with the correct number of decimals (you could also use the ROUND() function to limit your decimals):

Make a new geometry column with WKT text, e.g. `="POINT("&C2&" "&D2&")"` then save it as a CSV file.

Add your geometry back to QGIS with the Add Delimited Text Layer, selecting the new wkt geometry field:

Embarrassingly ugly but it does work and you could set up a spreadsheet template to automate it.

(Cue the other respondents with far more elegant PostGIS and Python solutions ... ;) )

• Thank you for the detailed response but I'm afraid I can't use this method. I could convert my polygon shapefile to a point shapefile but I would end up with almost 35,000 points and besides, I am editing a CONUS county shapefile so I would have to merge each point corresponding to the county FIPS number back into a polygon shapefile. Unless anyone knows how to do this, I'll have to find something else dealing directly with polygons. Thanks again for the excellent answer though, I'm sure it will help other readers. – Brandon Schmidt Nov 9 '13 at 14:06
• Had a feeling it wouldn't help but no worries. You might explore putting them into a format like Spatialite and then working with a WKT geometry field there; that would be much much more valid for such a large dataset. – Simbamangu Nov 9 '13 at 14:26