jazzband / geojson

Python bindings and utilities for GeoJSON
https://pypi.python.org/pypi/geojson/
BSD 3-Clause "New" or "Revised" License
924 stars 122 forks source link

[bug] geojson.dump return invalid geojson with GeometryCollection #231

Open ValentinBuira opened 3 weeks ago

ValentinBuira commented 3 weeks ago

When I open a valid GeometryCollection and try to save it to file it using geojson.dump, it returns an invalid geojson

when I try to check the geojson output in https://geojsonlint.com/ I got the error : Line 0: Parse error on line 1: ... 13.7896], [56.28535 -----------------------^ Expecting ',', ']', got 'EOF'

To reproduce run the following code that input valid geojson and return invalid one

import geojson

# Good GeometryCollection
geom_collection = geojson.loads('{"type": "GeometryCollection", "geometries": [{"type": "LineString", "coordinates": [[41.32783, 19.81862], [41.37011, 19.68489], [41.35593, 19.5291], [41.31564, 19.45047], [41.31698, 19.45541], [41.30002, 19.45309], [41.19279, 17.04484], [41.14822, 16.84536], [41.13198, 16.86733], [41.12896, 16.86577], [41.11783, 16.87027], [41.23546, 16.49948], [41.31536, 16.27859], [41.31552, 15.89734], [41.46559, 15.55557], [41.68299, 15.39257], [41.80765, 15.3507], [41.89429, 15.27508], [41.95727, 15.04231], [42.03222, 14.85102], [42.16682, 14.69806], [42.35342, 14.40771], [42.60714, 14.06908], [42.95388, 13.8823], [43.4333, 13.66147], [43.56752, 13.52099], [43.61666, 13.42972], [43.95976, 12.74509], [44.06403, 12.5741], [44.11661, 12.32757], [44.40734, 11.59768], [44.50623, 11.34227], [44.50703, 11.33609], [44.50768, 11.33101], [44.51083, 11.31925], [44.51617, 11.29974], [44.52384, 11.27935], [44.54853, 11.26403], [44.59095, 11.22977], [44.63275, 11.19748], [44.792, 11.14301], [44.92307, 11.11583], [45.0475, 11.13399], [45.15408, 11.09118], [45.42519, 10.95734], [45.46486, 10.93913], [45.50788, 10.83897], [45.5858, 10.84503], [45.66281, 10.90227], [45.75143, 10.98983], [45.84451, 11.00285], [45.99072, 11.12087], [46.14748, 11.09516], [46.30347, 11.24452], [46.49466, 11.38005], [46.54952, 11.51159], [46.63069, 11.55287], [46.66905, 11.62011], [46.7568, 11.63349], [46.82093, 11.53654], [46.87881, 11.46182], [46.92733, 11.44989], [46.94696, 11.41197], [46.96972, 11.43372], [47.01566, 11.50958], [47.08179, 11.47545], [47.19339, 11.41594], [47.24533, 11.39547], [47.27649, 11.48591], [47.38853, 11.77872], [47.51519, 12.08871], [47.59496, 12.16942], [47.66226, 12.176], [47.72326, 12.12852], [47.81184, 12.11981], [47.95609, 12.01874], [48.07963, 11.87152], [48.1354, 11.58139], [48.16963, 11.47605], [48.27487, 11.44612], [48.41284, 11.46932], [48.56719, 11.54412], [48.61719, 11.5738], [48.64879, 11.52891], [48.7298, 11.44302], [48.8053, 11.34722], [48.85546, 11.18849], [48.86592, 11.10387], [48.89166, 10.9953], [48.93636, 10.93342], [49.02969, 10.96525], [49.18991, 11.02009], [49.25194, 11.07791], [49.33976, 11.04553], [49.4288, 11.05467], [49.45058, 11.04179], [49.52079, 10.82234], [49.57028, 10.62662], [49.74008, 10.15652], [49.80289, 10.05012], [49.79525, 9.97494], [49.83064, 9.87664], [50.00753, 9.71242], [50.15322, 9.65521], [50.2282, 9.61077], [50.30342, 9.64166], [50.35559, 9.59202], [50.37255, 9.52984], [50.5134, 9.67578], [50.63543, 9.67653], [50.73196, 9.60857], [50.9228, 9.57894], [51.0725, 9.58501], [51.16939, 9.54699], [51.27599, 9.45378], [51.35468, 9.48761], [51.42356, 9.76006], [51.56269, 9.92084], [51.76119, 9.96665], [51.95332, 9.9956], [52.14426, 9.86566], [52.3288, 9.78532], [52.37998, 9.73549], [52.43225, 9.71658], [52.54884, 9.94701], [52.71477, 10.1901], [52.92015, 10.46033], [52.98652, 10.55154], [53.06661, 10.58202], [53.19061, 10.44171], [53.27009, 10.40786], [53.34573, 10.22635], [53.45108, 9.99662], [53.53501, 10.02439], [53.56534, 9.97392], [53.64959, 10.01731], [53.76919, 9.97092], [53.83396, 9.96867], [53.87717, 9.88964], [53.98557, 9.97784], [54.02029, 10.01652], [54.06432, 9.97526], [54.11841, 9.93358], [54.28591, 9.71093], [54.32372, 9.65028], [54.50223, 9.53174], [54.57259, 9.38479], [54.66852, 9.40024], [54.75322, 9.38992], [54.879, 9.30273], [54.97584, 9.27486], [55.06001, 9.32965], [55.29788, 9.29384], [55.46384, 9.30032], [55.49451, 9.42175], [55.53461, 9.5862], [55.54014, 9.70078], [55.50018, 9.74373], [55.40442, 9.9604], [55.36011, 10.16202], [55.3858, 10.34229], [55.35808, 10.62943], [55.32542, 10.94582], [55.41489, 11.37673], [55.45492, 11.913], [55.47935, 12.09952], [55.54105, 12.20624], [55.63535, 12.33581], [55.65243, 12.53184], [55.64892, 12.54145], [55.56583, 12.91016], [55.59182, 13.0019], [55.70878, 13.18625], [55.73993, 13.19131], [55.76873, 13.21738], [55.80419, 13.28632], [55.8436, 13.30822], [55.9163, 13.46116], [56.05616, 13.7151], [56.15337, 13.76097], [56.23763, 13.87817], [56.49335, 14.11451], [56.68645, 14.30831], [56.90176, 14.55994], [56.96338, 14.58143], [57.16122, 14.58466], [57.37449, 14.64226], [57.53429, 14.70036], [57.62325, 14.69949], [57.71812, 14.70786], [57.77074, 14.78584], [57.87324, 14.80199], [58.00074, 14.88022], [58.02745, 14.95899], [58.11715, 14.97951], [58.30971, 15.13277], [58.4077, 15.50334], [58.43656, 15.78708], [58.51553, 15.97447], [58.57298, 15.99864], [58.58991, 16.08288], [58.59663, 16.18335]]}, {"type": "LineString", "coordinates": [[48.17445, 16.33309], [48.20116, 16.25165], [48.29316, 16.04967], [48.29662, 15.94956], [48.25206, 15.8018], [48.2084, 15.62086], [48.18804, 15.42135], [48.20889, 15.24114], [48.13153, 15.01541], [48.08169, 14.77492], [48.0559, 14.64364], [48.12143, 14.57897], [48.22057, 14.48199], [48.25349, 14.33798], [48.2942, 14.30859], [48.27404, 14.25954], [48.19518, 14.08802], [48.16776, 14.01297], [48.20408, 13.97604], [48.2271, 13.85508], [48.27126, 13.72339], [48.26247, 13.64647], [48.29466, 13.62436], [48.40531, 13.52739], [48.45582, 13.44828], [48.5404, 13.44968], [48.57084, 13.45589], [48.57782, 13.40392], [48.60875, 13.28871], [48.69534, 13.01916], [48.83453, 12.71713], [48.87792, 12.54299], [48.87553, 12.42719], [48.90852, 12.31503], [49.00397, 12.1341], [49.01259, 12.12838], [49.08033, 12.12873], [49.13191, 12.12149], [49.15311, 12.07473], [49.21433, 12.11806], [49.32316, 12.10861], [49.36614, 12.10418], [49.40297, 12.01247], [49.44515, 11.86547], [49.48922, 11.78024], [49.51954, 11.56562], [49.50158, 11.51286], [49.50389, 11.35724], [49.45379, 11.11907], [49.48463, 10.93467], [49.62325, 10.50697], [49.69603, 10.21707], [49.74794, 10.15665], [49.8002, 10.0372], [49.79528, 9.97041], [49.802, 9.89274], [49.93268, 9.80767], [49.98235, 9.76676], [50.01436, 9.71085], [50.05432, 9.69225], [50.08024, 9.67034], [50.16572, 9.64716], [50.236, 9.5994], [50.27347, 9.64736], [50.30899, 9.62765], [50.35527, 9.57753], [50.37026, 9.56713], [50.37832, 9.5277], [50.44689, 9.61405], [50.51871, 9.67962], [50.56964, 9.68493], [50.62796, 9.68289], [50.66112, 9.63879], [50.68815, 9.61929], [50.74294, 9.60842], [50.85077, 9.59119], [51.10996, 9.58577], [51.18067, 9.5312], [51.24058, 9.46909], [51.30137, 9.45457], [51.33794, 9.47196], [51.35066, 9.51713], [51.38792, 9.6305], [51.43366, 9.77992], [51.50609, 9.86533], [51.53253, 9.92131], [51.57771, 9.92342], [51.68913, 9.9773], [51.77583, 9.96822], [51.86809, 9.9896], [51.96798, 9.99312], [52.05708, 9.91556], [52.16037, 9.85418], [52.21157, 9.82359], [52.25077, 9.83887], [52.33884, 9.77915], [52.38966, 9.72788], [52.40956, 9.692], [52.44538, 9.73099], [52.49712, 9.83237], [52.56326, 9.97931], [52.64087, 10.07725], [52.73442, 10.22018], [52.81473, 10.2804], [52.85465, 10.35594], [52.92556, 10.47402], [52.96024, 10.54052], [52.99906, 10.54706], [53.07968, 10.57275], [53.14018, 10.48724], [53.1985, 10.43514], [53.28371, 10.38624], [53.39136, 10.09925], [53.46087, 9.99249], [53.53938, 10.01847], [53.58259, 9.92125], [53.71358, 9.69565], [53.81199, 9.6431], [53.91896, 9.73676], [54.0768, 9.98044], [54.14427, 9.89277], [54.23394, 9.80408], [54.28825, 9.68555], [54.30011, 9.69835], [54.3585, 9.63362], [54.4612, 9.55232], [54.51095, 9.49647], [54.55442, 9.41362], [54.60121, 9.37799], [54.70064, 9.40189], [54.74988, 9.38788], [54.77524, 9.43211], [54.76483, 9.42008], [54.77871, 9.38358], [54.84195, 9.34453], [54.91068, 9.26697], [54.94786, 9.26306], [55.03712, 9.31976], [55.12933, 9.33702], [55.40558, 9.27399], [55.49639, 9.32518], [55.48798, 9.47549], [55.55487, 9.68875], [55.55908, 9.73011], [55.5071, 9.70337], [55.44375, 9.91395], [55.38708, 9.99783], [55.3711, 10.11606], [55.34455, 10.2128], [55.37059, 10.29651], [55.40145, 10.37396], [55.37648, 10.53155], [55.29923, 10.84021], [55.35769, 11.1382], [55.40665, 11.32727], [55.42826, 11.6046], [55.45079, 11.89709], [55.54605, 12.01852], [55.63819, 12.08644], [55.66462, 12.46196], [55.67289, 12.56461], [55.66139, 12.54228], [55.63011, 12.61592], [55.56305, 12.93035], [55.59111, 13.00083], [55.61381, 13.03404], [55.70123, 13.18432], [55.75512, 13.20365], [55.78781, 13.26294], [55.87893, 13.34047], [55.93544, 13.53826], [56.0817, 13.74021], [56.18765, 13.7896], [56.28535, 13.93323], [56.40116, 14.01289], [56.53111, 14.12768], [56.63717, 14.22382], [56.7167, 14.35245], [56.81166, 14.47715], [56.92349, 14.5675], [57.01724, 14.57647], [57.08426, 14.60928], [57.14232, 14.59225], [57.24832, 14.58662], [57.35058, 14.60895], [57.40423, 14.66621], [57.49786, 14.69018], [57.5803, 14.68979], [57.69169, 14.68714], [57.74273, 14.76032], [57.80083, 14.8067], [57.90946, 14.80362], [57.98293, 14.8493], [58.00433, 14.92882], [58.05488, 14.97633], [58.15223, 14.98991], [58.19752, 15.05936], [58.2563, 15.09466], [58.31248, 15.1312], [58.33316, 15.19196], [58.38511, 15.43704], [58.41478, 15.54495], [58.41603, 15.6657], [58.46849, 15.89163], [58.51821, 15.97103], [58.57426, 16.00235], [58.61224, 16.14864], [58.59663, 16.18335]]}]}')

f =  open("output_bad.geojson", "w")
# Bad GeometryCollection
geojson.dump(geom_collection, f)

Geojson library version : geojson==3.1.0

ValentinBuira commented 3 weeks ago

As a workaround since geojson.dumps works you can write the result of this function directly to file