Skip to main content

extract geojson from Mongo

Project description

mongo_to_geojson

A tool to help you extract spatial data from Mongo and format into GeoJSON

Why Use It

  • Dump entire collections, use filters (with or without projections), and/or use aggregation framework
  • For geometries, the tool can use either standard geojson geometry objects, or construct point geometries from other document properties 1
  • If you don't provide specific Mongo document properties to use for geometry, this tool attempts to infer geometry. The tool will look for standard geojson geometry objects or other common coordinate naming conventions2
  • Since data is streamed to output json file, this tool won't run out of memory when writing very large geojson files
  1. As long as the underlying Mongo value can be converted to a numeric value, this tool can use it. For example, a string value for an x coordinate will be converted just fine (ex: "x":"-104.33")

  2. Common coordinate names this tool looks for below. Case doesn't matter.

    • x, lng, long, longitude
    • y, lat, latitude

Installation

pip install mongo_to_geojson

CLI

The command line utility mongo2geojson.py is installed along with the pip package

Simplest use case: dump a collection to a properly formatted geojson file. As long as the documents in the worldcities collection have either a standard geometry property or a lat/lng values in common formats, no more is needed of you

 mongo2geojson.py mongodb://localhost/gisdata worldcities worldcities.json

Advanced use case: you have finally figured out an aggregate pipeline that answers all your questions. The output of the aggregate does not have a geometry object, nor does it have "standard" lat/long properties. Further complicating matters is that some of the output documents will have coordinates in fooX and fooY properties, and other documents in barX and barY properties. And you really don't want to try to copy & paste the aggregate pipeline into the command line - so you save it a file.

mongo2geojson.py mongodb://localhost/gisdata worldcities results.json --agg_pipeline=awesome_aggregate.json  --x_possible_names=fooX,barX y_possible_names=fooY,barY

Using JSON strings as commandline parameters can be quite a boondoggle owing to string escaping. For your own sanity, I'd suggest dumping query, projection, and/or agg_pipeline parameters to a file, and using those file paths as the cli parameters. Should you decide to use JSON strings, there are some subtle differences on how to do so between windows and unix.

unix:

--query='{"POP": {"$gte": 100000}}'

windows:

--query="{\"POP\": {\"$gte\": 100000}}"

API

Integrate into your own workflows

from mongo_to_geojson import mongo_to_geojson 

mongo_uri = 'mongodb://localhost/gisdata'
collection = 'worldcities'
output_geojson =  'worldcities.json'
query = {"POP": {"$gte": 100000}}
projection = {"CITY_NAME": 1, "x": 1, "y": 1, "_id": 0}

try:  
    mongo_to_geojson(mongo_uri,collection,output_geojson,query=query,projection=projection)  
except:  
    print('mongo_to_geojson no worky'))

Project details


Release history Release notifications | RSS feed

This version

0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mongo_to_geojson-0.2.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mongo_to_geojson-0.2-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file mongo_to_geojson-0.2.tar.gz.

File metadata

  • Download URL: mongo_to_geojson-0.2.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

File hashes

Hashes for mongo_to_geojson-0.2.tar.gz
Algorithm Hash digest
SHA256 8ec6b9b1277c95cc27fa2240c9c7d75e0e2d1f09a297c643c5c5fa8a67471970
MD5 6954b9a243d50cdd0722ab54185c7d58
BLAKE2b-256 9457632b7ec8a72a3c3d18f844eceb236845e708c090c61f43796a9b39b74468

See more details on using hashes here.

File details

Details for the file mongo_to_geojson-0.2-py3-none-any.whl.

File metadata

  • Download URL: mongo_to_geojson-0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

File hashes

Hashes for mongo_to_geojson-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d75adaa61b691f9fa86d02cac5a55dbda428840ee80692d040987bf1db0d7aab
MD5 6b1b0f5c3fb4feb019705ede46757a17
BLAKE2b-256 67d2d6d2f8a6666b93ae7a68f3a1e4ec885bfa5d7c745c5614be12232fe82d77

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page