7

My apologies if this is a repeat question, I searched here and couldn't find an answer. I have JSON data I've exported from Openpaths.cc which contains lat and lon values. However, the data is not in GeoJSON format so can't be read by OGR2OGR. Could anyone point me in the right direction on how to convert my data to GeoJSON? I'd ultimately like to display it with Leaflet. Here is what the data looks like:

[
{
    "lon": -73.97, 
    "device": "iPhone3,3", 
    "version": "1.1", 
    "t": 1381167616, 
    "lat": 40.66, 
    "alt": 67, 
    "os": "6.1.3"
}, 
{
    "lon": -73.96, 
    "device": "iPhone3,3", 
    "version": "1.1", 
    "t": 1381171200, 
    "lat": 40.66, 
    "alt": 45, 
    "os": "6.1.3"
} 

]

  • 1
  • I get a Failure: Unable to open datasource from ogr2ogr when trying that with my json data. – clhenrick Oct 8 '13 at 18:53
  • can you post file/code on jsfiddle or pastbin? – Mapperz Oct 8 '13 at 18:58
  • code? I'm just looking to change the above data format. The JSON above is a sample but that's the schema. – clhenrick Oct 8 '13 at 20:09
  • The easiest way is to save you layer as a GeoJson in QGIS without any code! – maxkpow Nov 14 '16 at 16:38
10

So this python script will take a json input file as detailed above and write properly formatted geojson to the output file.

run the script in terminal by doing python scriptname.py input_file.json output_file.json

#! usr/bin/env python

from sys import argv
from os.path import exists
import simplejson as json 

script, in_file, out_file = argv

data = json.load(open(in_file))

geojson = {
    "type": "FeatureCollection",
    "features": [
    {
        "type": "Feature",
        "geometry" : {
            "type": "Point",
            "coordinates": [d["lon"], d["lat"]],
            },
        "properties" : d,
     } for d in data]
}


output = open(out_file, 'w')
json.dump(geojson, output)

print geojson
  • When I try this with on the command line with my JSON file I get the error: File "json2geojson.py", line 28 print geojson ^ SyntaxError: invalid syntax Can you help me with this? – Stefan Sep 30 '14 at 19:36
  • Hi @Stefan what version of python are you using? if you comment out that line does it work? – clhenrick Oct 1 '14 at 19:36
  • 1
    I'm using python 2.7 so that may be an issue. Try changing import simplejson as json to import json – clhenrick Oct 16 '14 at 21:35
  • 1
    @Stefan can you post your json data somewhere so I can look at it? – clhenrick Oct 22 '14 at 18:05
  • 1
    Your data is formatted differently than mine was. If you remove the outer most { } and also remove "events:" but leave the [ ] then save the data file and run the script it should work. Here's what I mean: jsfiddle.net/chenrick/1cjh81hf – clhenrick Oct 23 '14 at 18:30
2

I would recommend you to follow:

1- Convert your json into csv using http://www.danmandle.com/blog/json-to-csv-conversion-utility/

2- Create a vrt file for your csv data using http://www.gdal.org/ogr/drv_csv.html

3- Use GDAL (ogr2ogr -f 'GeoJSON' output_file input_file)

4- Load output geojson file to your map using leaflet library

I hope it will help.

  • thanks Farhat, this solution does work. I also figured out a python script to accomplish this here – clhenrick Oct 10 '13 at 23:58
0

Converting ArcGIS JSON to GeoJSONThere in pure Browser

there are 2 ways you can do

1) TERRAFORMER

Note: use in node.js and use in browser are different, details see link

2) Esri/arcgis-to-geojson-utils

Esri does not provide the bundle.js, so you can NOT use this lib in browser directly

As of today, you can not load module in browser.

You can only use ESIR lib in node.js, not browser.

To use this esri lib in browser, you should use some bundler, please follow:

a) You need to compile all the module source file into a single bundle.js

rollup.js install by

npm install --global rollup

then go to your js lib root folder, find the entry point js file, in this case it is index.js

$ rollup index.js --format umd --name "esri_arcgis_to_geojson" --file bundle.js

You should find a new file bundle.js in your root directory.

Now in your browser html file, include this bundle.js file

<script src='.../.../.../bundle.js'>

You can use it now by

  // parse ArcGIS JSON, convert it to GeoJSON
  var geojson = esri_arcgis_to_geojson.arcgisToGeoJSON({
                            "x":-122.6764,
                            "y":45.5165,
                            "spatialReference": {
                              "wkid": 4326
                            }
                            });

 // take GeoJSON and convert it to ArcGIS JSON
  var arcgis = esri_arcgis_to_geojson.geojsonToArcGIS({
                            "type": "Point",
                              "coordinates": [45.5165, -122.6764]
                            });enter code here

Remember esri_arcgis_to_geojson is the name you named the lib

This becomes the global variable name, available in browser.

The trick is, bundle process add instant implement function like (function xx {}) here is the top part from bundle.js

  (function (global, factory) {
     typeof exports === 'object' && typeof module !== 'undefined' ? 
        factory(exports) :
      typeof define === 'function' && define.amd ? define(['exports'], 
         factory) :
      (factory((global.arcgis_to_geojson = {})));
      }(this, (function (exports) { 'use strict';

         ***ORIGINAL SOURCE CODE OF JS MODULE***

     })));

arcgis-to-geojson-utils

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.