# GeoJSON coordinate precision modification using Python 3 geojson module

Objective is to convert a large shapefile to GeoJSON using Python modules: `shapefile` and `geojson`.

Goal is to change the precision of coordinate points for a GeoJSON feature during the process of conversion.

Code

``````import geojson
import geojson_rewind

"""
Latitude coordinate precision by the actual cartographic scale they purport:
Decimal Places   Aprox. Distance    Say What?
1                10 kilometers      6.2 miles
2                1 kilometer        0.62 miles
3                100 meters         About 328 feet
4                10 meters          About 33 feet
5                1 meter            About 3 feet
6                10 centimeters     About 4 inches
7                1.0 centimeter     About 1/2 an inch
8                1.0 millimeter     The width of paperclip wire.
9                0.1 millimeter     The width of a strand of hair.
10               10 microns         A speck of pollen.
11               1.0 micron         A piece of cigarette smoke.
12               0.1 micron         You're doing virus-level mapping at this point.
13               10 nanometers      Does it matter how big this is?
15               0.1 nanometer      An atom. An atom! What are you mapping?

2018 geoJSON rfc7946 compliance check and reformat example
input = {
'properties': {'foo': 'bar'},
'geometry': {'type': 'Polygon',
'coordinates': [[[100.9999999999999999, 0.000000000000008],
[100, 1],
[101, 1],
[101, 0],
[100, 0]]]
},
'type': 'Feature'}
output = geojson_rewind.rewind(input)
print(input)
print(output)
"""

# # Data for just one feature : Example 01
# geometry = {'type': 'MultiPolygon', 'coordinates': [[[(-89.60879119731409, 53.71081602343624), (-89.60767788357202, 53.706211005316874), (-89.6047921074761, 53.70207943386505), (-89.61184455023869, 53.69378899468319), (-89.61315544976127, 53.68954433865021), (-89.62017788357203, 53.68128899468319), (-89.62083333333331, 53.66250000000003), (-89.62432623969181, 53.66160363091366), (-89.6255672878689, 53.64925333658857), (-89.62140062120223, 53.643877156575556), (-89.62026604546439, 53.64092000325524), (-89.62443271213105, 53.635543823242216), (-89.6255672878689, 53.631122843424514), (-89.62859937879772, 53.62721048990888), (-89.62973395453557, 53.62425333658857), (-89.62473534478079, 53.617803785536054), (-89.6313771565755, 53.61609937879778), (-89.63945617675779, 53.60890062120229), (-89.65326707628036, 53.60767211914066), (-89.65473395453557, 53.601956176757845), (-89.65776604546438, 53.59804382324222), (-89.6583333333333, 53.59583333333337), (-89.6583333333333, 53.5916666666667), (-89.65886281331377, 53.585251702202726), (-89.66197052001951, 53.58141496446401), (-89.66327260335284, 53.57588636610246), (-89.66766594780813, 53.57451799180776), (-89.67275170220266, 53.57863718668623), (-89.67724829779729, 53.579696146647166), (-89.68239254421655, 53.583862813313836), (-89.68974829779728, 53.5828038533529), (-89.693585035536, 53.579696146647166), (-89.69999999999997, 53.5791666666667), (-89.70061764187281, 53.581326802571645), (-89.70354902479382, 53.58533986409508), (-89.70513678656681, 53.59624837239586), (-89.71457672119138, 53.58805660671661), (-89.71967451307506, 53.591780598958366), (-89.72145097520614, 53.59799346923831), (-89.72438235812714, 53.60200653076175), (-89.7256176418728, 53.60490332709421), (-89.71728430853948, 53.61450653076175), (-89.71604902479382, 53.62363213433163), (-89.72299346923826, 53.625617641872864), (-89.7280078464084, 53.62928059895837), (-89.72978430853948, 53.63549346923831), (-89.73688235812715, 53.64367319742842), (-89.7381176418728, 53.647993469238315), (-89.74110514322915, 53.652083333333366), (-89.7381176418728, 53.656173197428416), (-89.73688235812715, 53.66049346923831), (-89.73350630658994, 53.66438404379643), (-89.72637939453125, 53.665931701660156), (-89.70256805419922, 53.672584533691406), (-89.68986511230469, 53.68012237548828), (-89.68383026123047, 53.68504333496094), (-89.6921157836914, 53.688621520996094), (-89.67433166503906, 53.69743728637695), (-89.64100646972656, 53.70138931274414), (-89.63653564453125, 53.69856643676758), (-89.62765502929688, 53.6990852355957), (-89.61170959472656, 53.70957565307617), (-89.61009216308594, 53.710636138916016), (-89.60879119731409, 53.71081602343624)], [(-89.67340850830078, 53.6709098815918), (-89.6692886352539, 53.676090240478516), (-89.6565170288086, 53.68476867675781), (-89.65116882324219, 53.683982849121094), (-89.6382827758789, 53.687400817871094), (-89.63935852050781, 53.689022064208984), (-89.65470123291016, 53.68999099731445), (-89.66403198242188, 53.68833541870117), (-89.66793823242188, 53.68726348876953), (-89.66812896728516, 53.68361282348633), (-89.67532348632812, 53.67848587036133), (-89.68736267089844, 53.676422119140625), (-89.69098663330078, 53.67329025268555), (-89.69020080566406, 53.66619110107422), (-89.68132019042969, 53.66671371459961), (-89.67340850830078, 53.6709098815918)]], [[(-89.67880433775812, 53.723535958957626), (-89.67141486273869, 53.72982211642798), (-89.66871100531682, 53.72851121690542), (-89.66705442802152, 53.72735409879464), (-89.67213439941406, 53.724815368652344), (-89.66834259033203, 53.72360610961914), (-89.66409658297879, 53.725288046049926), (-89.66274583604599, 53.72434455023875), (-89.66037767198348, 53.72565544976132), (-89.65951913930577, 53.72625509831097), (-89.64420318603516, 53.72841262817383), (-89.6414423547849, 53.72967352523328), (-89.63954433865015, 53.72982211642798), (-89.63657327156653, 53.73189728038371), (-89.63476996056178, 53.73272086912821), (-89.61454433865015, 53.73398878309465), (-89.60474819607202, 53.742322116427985), (-89.59371100531682, 53.74101121690541), (-89.58962232801647, 53.738155449761315), (-89.58339216444224, 53.73623148600264), (-89.5868445502387, 53.73128899468319), (-89.58825615776907, 53.71325615776913), (-89.59806179819986, 53.712488616184615), (-89.59526062011719, 53.71424102783203), (-89.59713745117188, 53.72227478027344), (-89.61832427978516, 53.71513366699219), (-89.61885070800781, 53.7197151184082), (-89.62229919433594, 53.720008850097656), (-89.62976837158203, 53.71717834472656), (-89.63399505615234, 53.71017074584961), (-89.64031982421875, 53.70709228515625), (-89.6484375, 53.7065544128418), (-89.64558410644531, 53.7167854309082), (-89.64708709716797, 53.717498779296875), (-89.6510009765625, 53.7164306640625), (-89.65392303466797, 53.704830169677734), (-89.67093658447266, 53.70354461669922), (-89.66605377197266, 53.71579360961914), (-89.67189025878906, 53.71475601196289), (-89.6740493774414, 53.711544036865234), (-89.67522430419922, 53.70979309082031), (-89.67643737792969, 53.701358795166016), (-89.6839828491211, 53.69692611694336), (-89.68656921386719, 53.699031829833984), (-89.68279266357422, 53.70513153076172), (-89.6849136352539, 53.71613693237305), (-89.67875671386719, 53.72333908081055), (-89.67880433775812, 53.723535958957626)]]]}
# properties = {'HYBAS_ID': 7120182382, 'NEXT_DOWN': 7129002641, 'NEXT_SINK': 7120022840, 'MAIN_BAS': 7120022840, 'DIST_SINK': 424.6, 'DIST_MAIN': 424.6, 'SUB_AREA': 86.4, 'UP_AREA': 478.4, 'PFAF_ID': 713440760100, 'SIDE': 'R', 'LAKE': 0, 'ENDO': 0, 'COAST': 0, 'ORDER': 3, 'SORT': 55980}

# Data for just one feature : Example 02
geometry = {'type': 'Polygon', 'coordinates': [[(-122.44999999999996, 53.91250000000004), (-122.45242445203989, 53.912853325737885), (-122.45590888129337, 53.91631334092885), (-122.46075778537323, 53.91701999240455), (-122.46424221462671, 53.92048000759552), (-122.46666666666663, 53.92083333333337), (-122.46734381781681, 53.93233557807078), (-122.47299363878035, 53.933950975206194), (-122.47843119303381, 53.93829989963111), (-122.47990180121525, 53.95336405436201), (-122.48966030544702, 53.95478430853953), (-122.49367302788625, 53.957715691460535), (-122.50216030544702, 53.9589509752062), (-122.50617302788625, 53.961882358127205), (-122.5083333333333, 53.962500000000034), (-122.50776604546438, 53.96471048990889), (-122.49640062120223, 53.976956176757845), (-122.49517211914059, 53.99076707628042), (-122.49166666666663, 53.9916666666667), (-122.48533969455292, 53.99228413899741), (-122.48132697211368, 53.99521586100262), (-122.47162746853296, 53.996627468533006), (-122.46988491482202, 54.00860087076825), (-122.46466030544701, 54.004784138997415), (-122.45786404079858, 54.00354919433595), (-122.45115865071611, 54.008447265625016), (-122.44938252766923, 54.014660305447066), (-122.43811747233069, 54.027006361219634), (-122.43688252766924, 54.039660305447065), (-122.43395080566403, 54.04367302788631), (-122.43271586100256, 54.05216030544707), (-122.42978413899736, 54.0561730278863), (-122.42916666666663, 54.05833333333335), (-122.42916666666663, 54.06250000000002), (-122.42585076226125, 54.0633507622613), (-122.42499999999997, 54.066666666666684), (-122.4083333333333, 54.066666666666684), (-122.40743374294702, 54.063161214192725), (-122.39362284342444, 54.061932712131096), (-122.38971048990882, 54.05890062120228), (-122.37695617675777, 54.057766045464426), (-122.37304382324216, 54.05473395453561), (-122.35612284342444, 54.05359937879776), (-122.35221048990881, 54.050567287868944), (-122.3477895100911, 54.04943271213109), (-122.33554382324215, 54.03806728786894), (-122.32980448404945, 54.03659430609811), (-122.32853393554684, 54.0176456027561), (-122.32304382324216, 54.01306728786894), (-122.28841451009112, 54.01189744737415), (-122.28664923773869, 54.00501742892797), (-122.28112284342444, 54.00359937879776), (-122.27177056206594, 53.995266045464426), (-122.26445617675778, 53.99640062120228), (-122.26054382324214, 53.999432712131096), (-122.25416666666663, 54.00000000000002), (-122.25381334092879, 53.9975755479601), (-122.2503533257378, 53.994091118706635), (-122.24999999999996, 53.9916666666667), (-122.25055643717444, 53.97178700764977), (-122.25516594780812, 53.970351325141095), (-122.2608727349175, 53.97497338189022), (-122.27030368381072, 53.96474829779734), (-122.27136298285586, 53.960251702202726), (-122.28280368381073, 53.94808163113068), (-122.2833333333333, 53.94583333333337), (-122.28749999999997, 53.94583333333337), (-122.29166666666663, 53.94583333333337), (-122.29252760145395, 53.9488440619575), (-122.29630262586802, 53.95061764187287), (-122.30200636121958, 53.9464509752062), (-122.31170586480032, 53.94503919813371), (-122.31311747233069, 53.935339864095084), (-122.3211300320095, 53.92610643174917), (-122.32839830186629, 53.92402835422095), (-122.32997775607636, 53.913176303439705), (-122.35632697211368, 53.9118823581272), (-122.36450636121958, 53.90478430853953), (-122.38796929253469, 53.90354902479387), (-122.39367302788625, 53.90771569146054), (-122.41049363878035, 53.908950975206196), (-122.41450636121958, 53.9118823581272), (-122.42299363878034, 53.913117641872866), (-122.42700636121958, 53.91604902479387), (-122.43380262586803, 53.91728430853953), (-122.43950636121959, 53.913117641872866), (-122.44999999999996, 53.91250000000004)]]}
properties = {'HYBAS_ID': 7120174760, 'NEXT_DOWN': 7120174110, 'NEXT_SINK': 7120016260, 'MAIN_BAS': 7120016260, 'DIST_SINK': 836.0, 'DIST_MAIN': 836.0, 'SUB_AREA': 179.1, 'UP_AREA': 2860.6, 'PFAF_ID': 783294030000, 'SIDE': 'M', 'LAKE': 0, 'ENDO': 0, 'COAST': 0, 'ORDER': 2, 'SORT': 26148}

# Empty list for collecting features in a loop
features = []
# Need to scale down the ridiculous 14 digit precision
mod  = getattr(geojson, geometry['type'])
new_geometry = mod(geometry['coordinates'], precision=2)
feature = {"type": "Feature", "properties": properties, "geometry": new_geometry}

# Compliance check
compliant_feature_string = geojson_rewind.rewind(geojson.dumps(feature))
features.append(compliant_feature_object)

ftr_collection = {"type": "FeatureCollection", "features": features}
showonscreen = geojson.dumps(ftr_collection, indent=2)
# showonscreen = geojson.dumps(ftr_collection)
print(showonscreen)

# # save to file if you need to
# outfile = 'path/filename.geojson'
# with open(outfile, "w", encoding="utf-8") as fo:
#     geojson.dump(ftr_collection, fo, indent=2)
``````

Output

``````Example 01 Data
{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "MultiPolygon", "coordinates": [[[[-89.61, 53.71], [-89.61, 53.71], [-89.61, 53.71], [-89.63, 53.7], [-89.64, 53.7], [-89.64, 53.7], [-89.67, 53.7], [-89.69, 53.69], [-89.68, 53.69], [-89.69, 53.68], [-89.7, 53.67], [-89.73, 53.67], [-89.73, 53.66], [-89.74, 53.66], [-89.74, 53.66], [-89.74, 53.65], [-89.74, 53.65], [-89.74, 53.64], [-89.73, 53.64], [-89.73, 53.63], [-89.72, 53.63], [-89.72, 53.62], [-89.72, 53.61], [-89.73, 53.6], [-89.72, 53.6], [-89.72, 53.6], [-89.72, 53.59], [-89.71, 53.59], [-89.71, 53.6], [-89.7, 53.59], [-89.7, 53.58], [-89.7, 53.58], [-89.69, 53.58], [-89.69, 53.58], [-89.68, 53.58], [-89.68, 53.58], [-89.67, 53.58], [-89.67, 53.57], [-89.66, 53.58], [-89.66, 53.58], [-89.66, 53.59], [-89.66, 53.59], [-89.66, 53.6], [-89.66, 53.6], [-89.65, 53.6], [-89.65, 53.61], [-89.64, 53.61], [-89.63, 53.62], [-89.62, 53.62], [-89.63, 53.62], [-89.63, 53.63], [-89.63, 53.63], [-89.62, 53.64], [-89.62, 53.64], [-89.62, 53.64], [-89.63, 53.65], [-89.62, 53.66], [-89.62, 53.66], [-89.62, 53.68], [-89.61, 53.69], [-89.61, 53.69], [-89.6, 53.7], [-89.61, 53.71], [-89.61, 53.71]], [[-89.67, 53.67], [-89.68, 53.67], [-89.69, 53.67], [-89.69, 53.67], [-89.69, 53.68], [-89.68, 53.68], [-89.67, 53.68], [-89.67, 53.69], [-89.66, 53.69], [-89.65, 53.69], [-89.64, 53.69], [-89.64, 53.69], [-89.65, 53.68], [-89.66, 53.68], [-89.67, 53.68], [-89.67, 53.67]]], [[[-89.68, 53.72], [-89.68, 53.72], [-89.68, 53.72], [-89.68, 53.71], [-89.69, 53.7], [-89.68, 53.7], [-89.68, 53.7], [-89.68, 53.71], [-89.67, 53.71], [-89.67, 53.71], [-89.67, 53.72], [-89.67, 53.7], [-89.65, 53.7], [-89.65, 53.72], [-89.65, 53.72], [-89.65, 53.72], [-89.65, 53.71], [-89.64, 53.71], [-89.63, 53.71], [-89.63, 53.72], [-89.62, 53.72], [-89.62, 53.72], [-89.62, 53.72], [-89.6, 53.72], [-89.6, 53.71], [-89.6, 53.71], [-89.59, 53.71], [-89.59, 53.73], [-89.58, 53.74], [-89.59, 53.74], [-89.59, 53.74], [-89.6, 53.74], [-89.61, 53.73], [-89.63, 53.73], [-89.64, 53.73], [-89.64, 53.73], [-89.64, 53.73], [-89.64, 53.73], [-89.66, 53.73], [-89.66, 53.73], [-89.66, 53.72], [-89.66, 53.73], [-89.67, 53.72], [-89.67, 53.72], [-89.67, 53.73], [-89.67, 53.73], [-89.67, 53.73], [-89.68, 53.72]]]]}, "properties": {"HYBAS_ID": 7120182382, "NEXT_DOWN": 7129002641, "NEXT_SINK": 7120022840, "MAIN_BAS": 7120022840, "DIST_SINK": 424.6, "DIST_MAIN": 424.6, "SUB_AREA": 86.4, "UP_AREA": 478.4, "PFAF_ID": 713440760100, "SIDE": "R", "LAKE": 0, "ENDO": 0, "COAST": 0, "ORDER": 3, "SORT": 55980}}]}

Example 02 Data
{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "MultiPolygon", "coordinates": [[[53.91, -122.45], [-122.45, 53.91], [-122.46, 53.92], [-122.46, 53.92], [-122.46, 53.92], [-122.47, 53.92], [-122.47, 53.93], [-122.47, 53.93], [-122.48, 53.94], [-122.48, 53.95], [-122.49, 53.95], [-122.49, 53.96], [-122.5, 53.96], [-122.51, 53.96], [-122.51, 53.96], [-122.51, 53.96], [-122.5, 53.98], [-122.5, 53.99], [-122.49, 53.99], [-122.49, 53.99], [-122.48, 54.0], [-122.47, 54.0], [-122.47, 54.01], [-122.46, 54.0], [-122.46, 54.0], [-122.45, 54.01], [-122.45, 54.01], [-122.44, 54.03], [-122.44, 54.04], [-122.43, 54.04], [-122.43, 54.05], [-122.43, 54.06], [-122.43, 54.06], [-122.43, 54.06], [-122.43, 54.06], [-122.42, 54.07], [-122.41, 54.07], [-122.41, 54.06], [-122.39, 54.06], [-122.39, 54.06], [-122.38, 54.06], [-122.37, 54.05], [-122.36, 54.05], [-122.35, 54.05], [-122.35, 54.05], [-122.34, 54.04], [-122.33, 54.04], [-122.33, 54.02], [-122.32, 54.01], [-122.29, 54.01], [-122.29, 54.01], [-122.28, 54.0], [-122.27, 54.0], [-122.26, 54.0], [-122.26, 54.0], [-122.25, 54.0], [-122.25, 54.0], [-122.25, 53.99], [-122.25, 53.99], [-122.25, 53.97], [-122.26, 53.97], [-122.26, 53.97], [-122.27, 53.96], [-122.27, 53.96], [-122.28, 53.95], [-122.28, 53.95], [-122.29, 53.95], [-122.29, 53.95], [-122.29, 53.95], [-122.3, 53.95], [-122.3, 53.95], [-122.31, 53.95], [-122.31, 53.94], [-122.32, 53.93], [-122.33, 53.92], [-122.33, 53.91], [-122.36, 53.91], [-122.36, 53.9], [-122.39, 53.9], [-122.39, 53.91], [-122.41, 53.91], [-122.41, 53.91], [-122.42, 53.91], [-122.43, 53.92], [-122.43, 53.92], [-122.44, 53.91], [-122.45, 53.91]]]}, "properties": {"HYBAS_ID": 7120174760, "NEXT_DOWN": 7120174110, "NEXT_SINK": 7120016260, "MAIN_BAS": 7120016260, "DIST_SINK": 836.0, "DIST_MAIN": 836.0, "SUB_AREA": 179.1, "UP_AREA": 2860.6, "PFAF_ID": 783294030000, "SIDE": "M", "LAKE": 0, "ENDO": 0, "COAST": 0, "ORDER": 2, "SORT": 26148}}]}
``````

Issue

The change of precision works fine, but the nesting is somewhat changed now. For example, notice the substitution of parentheses with square brackets, i.e. '()' with '[]' in the second output. This creates further issues down the lane while using `geojson.dump` to write to file and geojson validator invalidates the data.

Question

How to retain the structure identical to the input object (i.e., 'f' in the example) and implement the precision setting?

• Your coordinates validate ok in geojson.io Commented Oct 24, 2021 at 12:14
• I don't understand your problem. If you save the file and use GeoJSON valiidator, for example, the geojson is ok
– gene
Commented Oct 24, 2021 at 12:27
• The specifications for GeoJSON (RFC 7946, datatracker.ietf.org/doc/html/rfc7946) has some examples (datatracker.ietf.org/doc/html/rfc7946#appendix-A.1). The `[]` brackets are correct and valid. Commented Oct 24, 2021 at 12:29
• What issues do you get down the lane? `geojson.MultiPolygon(coordinates, precision=3)` works as intended (github.com/jazzband/geojson#multipolygon). And `geojson.dump([new_geometry], file)` works fine for me too. Commented Oct 24, 2021 at 12:56
• Thanks for your comments. Perhaps my experimental set-up was not perfect. I edited the original text to include the full code, along with `properties` and `geometry`. There are two test cases: Example 01 data is validated by `https://geojsonlint.com/`, Example 02 data fails. Commented Oct 24, 2021 at 22:06

It was a typo. Need to automate the `geojson.attribute`. In the 2nd case, it was failing because the `geojson.Polygon` needs to be called and I was calling `geojson.Multipolygon` as for the 1st case.
``````# Need to scale down the ridiculous 14 digit precision