GIS - GeoJSON

Explore GeoJSON for use in geographical information systems (GIS)

In the following we will show you how get geojson polygons for 600/1200s traveltime with walk routing in the center of Berlin in JavaScript, Python and on the command line with Curl.

JavaScript API

To generate GeoJson with the Javascript API, we will write a short JS script. The first thing we must do in this script is to create a TargomoClient object. This JavaScript object is used to send out the request for geojson polygons. Get your own Targomo API key, and check out our other region endpoints here, in case you do not want to use the westcentraleurope endpoint.

const client = new tgm.TargomoClient('westcentraleurope', '__targomo_key_here__');

We also need a travel options object and to switch from the default polygon serialization to the GeoJson version with the serializer parameter. Check out this documentation, for other parameters you can configure.

const options = {
    travelType: 'walk',
    travelEdgeWeights: [600, 1200],
    srid: 4326,
    serializer: 'geojson'
};

And finally, we have to define a source location.

const sources = [{id: 1, lat: 52.52, lng:  13.405 }];

Now we can use the polygon service to send the request and even use the default GeoJson layer from Leaflet to display the polygons. Take a look at this official Leaflet documentation to learn more about geojson in Leaflet, and how to apply other styles if you’d like to do so.

client.polygons.fetch(sources, options).then((result) => {
    L.geoJson(result, {}).addTo(map);
});

Check out this working code example to see everything put together.

Python API

If you haven’t yet, install Python3.

Install the Targomo Python library using pip install targomo_python.

Test the install with python -m targomo_python.cli -h, this shows the cli usage and help.

python -m targomo_python.cli \
    --travelType walk \
    --travelTime 600 1200 \
    --source "52.52;13.405" \
    --outputDir data/ \
    --outputFilename test.geojson \
    --srid 4326 \
    --serviceKey <your service key> \
    --serviceUrl https://api.targomo.com/westcentraleurope/

You can then drag and drop the generated file to QGIS or any other GIS as long as it supports GeoJSON. In case GeoJson is not supported, you can transform the result very easily with ogr2ogr to, let’s say an ESRI Shapefile:

/usr/local/bin/ogr2ogr -f "ESRI Shapefile" 'data/output_filename.shp' 'data/input_filename.geojson'

Command line usage

If you prefer to do things on the command line you can use cURL. But you need generate the request URL, e.g. the polygon configuration, yourself. So let’s define a configuration for 1800s walking in Berlin center.

{
    "sources": [{
        "lat": 52.52,
        "lng": 13.405,
        "id": "id123",
        "tm": {
            "walk": {}
        }
    }],
    "polygon": {
        "serializer": "geojson",
        "srid": 4326,
        "values": [1800]
    }
}

And the configuration in a cURL POST request (`--compressed` to unzip the response)
curl --compressed -X POST \
  'https://service.targomo.com/westcentraleurope/v1/polygon_post?key=__targomo_key_here__' \
  -H 'Content-Type: application/json' \
  -d '{
    "sources": [{
        "lat": 52.52,
        "lng": 13.405,
        "id": "id123",
        "tm": {
            "walk": {}
        }
    }],
    "polygon": {
        "serializer": "geojson",
        "srid": 4326,
        "values": [1800]
    }
}'

The response data are in a json key `data`, so if we use jq, we can access this data directly and pipe to file
curl --compressed -X POST \
  'https://service.targomo.com/westcentraleurope/v1/polygon_post?key=__targomo_key_here__' \
  -H 'Content-Type: application/json' \
  -d '{
    "sources": [{
        "lat": 52.52,
        "lng": 13.405,
        "id": "id123",
        "tm": {
            "walk": {}
        }
    }],
    "polygon": {
        "serializer": "geojson",
        "srid": 4326,
        "values": [1800]
    }
}' | jq .data > berlin_30_min_walk.geojson