16

I want to write a geosjon object with the type <class 'geojson.feature.Feature'> into a .geosjon file. Therefore I tried to use

with open(test.geosjon, 'w') as outfile:
     geojson.dump(geosjon_geometries, outfile)

But i get the error TypeError: coercing to Unicode: need string or buffer, tuple found I figured out that with this function a dict is needed to write it into a geosjon file. Is there another possibility to write a geojson feature in a file?

The function looks like:

def write_json(self, features):
    # feature is a shapely geometry feature
    geom_in_geojson = geojson.Feature(geometry=features, properties={})
    tmp_file = tempfile.mkstemp(suffix='.geojson')
    with open(tmp_file, 'w') as outfile:
        geojson.dump(geom_in_geojson, outfile)
    return tmp_file

The input is a shapely geometry, e.g. MultiLineString or LineString

3
  • Can you show the logic for the class? I would recommend working with the json module.
    – crmackey
    Commented Jan 20, 2015 at 15:28
  • I added the function to the question
    – Martin
    Commented Jan 20, 2015 at 15:34
  • I found the solution. It is working like this. The problem was the funktion tempfile.mkstep(). This returns a touple. So for the correct path to the temporary file it has to be used tmp_file[1].
    – Martin
    Commented Jan 20, 2015 at 15:42

3 Answers 3

30

Dumping a list of features directly does not create a valid GeoJSON file.

To create valid GeoJSON:

  1. Create a list of features (where each feature has geometry and optional properties)
  2. Create a collection (e.g. FeatureCollection) with those features
  3. Dump the collection to a file.

e.g.

from geojson import Point, Feature, FeatureCollection, dump

point = Point((-115.81, 37.24))

features = []
features.append(Feature(geometry=point, properties={"country": "Spain"}))

# add more features...
# features.append(...)

feature_collection = FeatureCollection(features)

with open('myfile.geojson', 'w') as f:
   dump(feature_collection, f)

Output:

{
    "type": "FeatureCollection",
    "features": [{
        "geometry": {
            "type": "Point",
            "coordinates": [-115.81, 37.24]
        },
        "type": "Feature",
        "properties": {
            "country": "Spain"
        }
    }]
}
1
  • thank for a good answer. GeoJson can accept a shapely geometry object.
    – beahacker
    Commented Apr 22, 2019 at 11:26
10

To write a geojson object to a temporary file this function can be used:

import geojson
import tempfile

def write_json(self, features):
   # feature is a shapely geometry type
   geom_in_geojson = geojson.Feature(geometry=features, properties={})
   tmp_file = tempfile.mkstemp(suffix='.geojson')
   with open(tmp_file[1], 'w') as outfile:
      geojson.dump(geom_in_geojson, outfile)
   return tmp_file[1]
2
  • can you break it down, I want to store geopandas GoDataFrame as temporary file, I changed the code as: import geojson import tempfile def write_json(self, features): geom_in_geojson = geojson.Feature(geometry=df_new['geometry'], properties={df_new['pod']}) tmp_file = tempfile.mkstemp(suffix='.geojson') print(tmp_file[1]) with open(tmp_file[1], 'w') as outfile: print(tmp_file[1]) geojson.dump(geom_in_geojson, outfile) return tmp_file[1] ## but it did not worked @Martin Commented May 18, 2021 at 20:12
  • what are self, features in def write_json(self, features): Commented May 18, 2021 at 20:28
2

Supposing that geom is a shapely geometry object, you can use

from shapely.geometry import mapping
import json

geojson = json.dumps(mapping(geom)) 
0

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.