4

I have tons of pictures which I am showing in markers on a LeafletJs map and currently have a fixed GeoJSON file. So I want to know is there any easy way to dynamically add new features to the GeoJSON file?

I was thinking a script(Php/JS) which scans a folder for new images, then gets the coordinates from a GPX file based on the image name and then add those to the GeoJSON feature collection. Is this kind of solution possible?

somnium
  • 41
  • 1
  • 5
  • geojson.io could one of the possibilities – poshan Jul 01 '14 at 05:20
  • @poshan Maybe my question was a bit inaccurate, but I am looking ways to add data to my geojson file without manually adding those – somnium Jul 01 '14 at 05:35
  • @somnium this is a good question - I updated the title slightly to make it more relevant. The main question is how to interact with your GeoJSON file programatically - once you figure this out you can add as much data as necessary – Stephen Lead Jul 01 '14 at 05:43
  • You might need to specify the technology you intend to use. I think it could be done programatically in C# fairly easily, for example, using the JavaScriptSerializer class. – drunkenwagoner Jul 01 '14 at 06:18
  • 1
    @somnion, Your question is currently vague. What exactly do you mean when you say that you "want to dynamically add new points to the GeoJSON"? Do you want to save the updated GeoJSON file after you alter it? Where is it being read? What programming API/Language do you want to use? – Devdatta Tengshe Jul 01 '14 at 06:26
  • @DevdattaTengshe I want a simple way to add data to the GeoJSON file and as I'm doing this for a friend who isn't really familiar with computers, the process should be as automated as possible. Data from the GeoJSON file is used to show markers on a LeafletJS map which has the image links. I was thinking maybe this could be done with PHP? – somnium Jul 01 '14 at 06:59
  • @somnium: Your Question is tautological. The only way to dynamically add features to a GeoJSON file, it to dynamically edit it. How you edit it, is the real question. We can't really answer that question unless we know more. You can edit it using Php, javaScript, .NET, bash scripts, Assembly level language, and so on. Unless we know more about your situation, we can help you. – Devdatta Tengshe Jul 01 '14 at 07:18
  • @DevdattaTengshe I was thinking a script(Php/JS) which scans a folder for new images, then gets the coordinates from a gpx file based on the image name and then adds those to the GeoJSON. Is this kind of solution possible? – somnium Jul 01 '14 at 07:29
  • 1
    @somnium: yes it is possible. I would suggest that you edit this question, and put in the relevant parts of your comments in it. Please see this post on tips to improve the question http://meta.gis.stackexchange.com/questions/3349/how-to-frame-a-good-question – Devdatta Tengshe Jul 01 '14 at 07:32

1 Answers1

5

You can write a script that reads the JSON file, appends the new features and write it back. That way when the map is loaded, it reads the new GeoJSON file.

import json

# Load existing data
with open('test.json') as f:
  data = json.load(f)

# Add data 
feature = {}
feature['type'] = 'Feature'
feature['geometry'] = {'type': 'Point',
                       'coordinates': [10, 10],
                       }
feature['properties'] =  {'prop0': 'value1'}
data['features'].append(feature)

# Write JSON file with new data
with open('test.json', 'w') as f:
  f.write(json.dumps(data))
spatialthoughts
  • 6,106
  • 28
  • 49