1

The .CSV file has 6 indents, we need to append all the indent with GeoJSON file and updated file ready for open in QGIS

    import json
    import copy
    import csv
    import sys

    #print(sys.argv)

    def read_in_csv(csvfile):
    with open (csvfile.csv, 'rb') as f:
        reader = csv.reader(f)
        header = (f.next()).strip('\n').strip('\t')
        properties = list(reader)
        flatlist_properties = [x for y in properties for x in y]
    return flatlist_properties, header 

    def append_properties_to_geojson(csvfile, geojsonfile):
        data = json.loads(open(geojsonfile).read())
        data = copy.deepcopy(data)
        values_to_add = read_in_csv(csvfile)[0]
        try:
            for entry in range(len(data["features"])):
            data["features"][entry]["properties"][read_in_csv(csvfile)[1]] = values_to_add[entry]
    except:
        print ("Note: Your CSV and geojson features do not contain the same number of values")
    return data 

    if __name__ == "__main__":
    csvfile = sys.argv[1]
    geojsonfile = sys.argv[2]

    with open('updated_data.json', 'w') as outfile:
        json.dump(append_properties_to_geojson(csvfile, geojsonfile), outfile, indent=6)

    print ("Data dumped into 'updated_data.json'")

# Usage: python add_csv_to_geojson.py csvfile.csv geojsonfile.json

Error show

    ['add_csv_to_geojson.py', 'csvfile.csv', 'geojsonfile.geojson']
    Traceback (most recent call last):
    File "add_csv_to_geojson.py", line 37, in <module>
    json.dump(append_properties_to_geojson(csvfile, geojsonfile), outfile,  indent=4)
    File "add_csv_to_geojson.py", line 22, in append_properties_to_geojson
    data = json.loads(open(geojsonfile).read())
    File "C:\Program Files\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
    File "C:\Program Files\Python37\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    File "C:\Program Files\Python37\lib\json\decoder.py", line 355, in  raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
3

1 Answer 1

2

I would recommend geopandas. Attribute Join documentation.

Here is how I would do it (Note: I haven't tried your data yet):

import pandas
import geopandas

gdf = geopandas.read_file(...) # geojson file
pdf = pandas.read_csv(...) # CSV file

joined_gdf = gdf.merge(pdf, on="state_name")
joined_gdf.to_file(..., driver="GeoJSON")
4
  • Thank you, Its works on shapefile only
    – Noor Hasan
    Commented Apr 21, 2020 at 14:51
  • Should work for geojson as well.
    – snowman2
    Commented Apr 21, 2020 at 15:23
  • @snowman2....When I work with the geojson file the error shows "fiona.errors.DriverError: 'geojsonfile.geojson' not recognized as a supported file format." can you please fix this issues to read and merge the geojson file
    – Noor Hasan
    Commented Apr 23, 2020 at 20:37
  • I just checked your geojson file and it is invalid geosjon. It starts with var geoStates = and it is missing , in some places. Once you fix that, then it should work.
    – snowman2
    Commented Apr 24, 2020 at 2:18

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.