Code Monkey home page Code Monkey logo

geojson-mongo-import.py's Introduction

geojson-mongo-import.py

Fast GeoJSON file import into MongoDB using Python

The standard mongoimport tool provided with MongoDB does not recognize GeoJSON files without first editing them manually or running them through a script. Using this Python program instead of mongoimort takes care of that. It is assumed that the GeoJSON features will be inserted as individual documents within the target collection. The other assumption is that your import file is well-formed GeoJSON consisting of a FeatureCollection. This script should work with most standard geometry types supported in MongoDB, including Point, LineString, Polygon, along their respective multipart equivalents, such as MultiPolygon.

Before running ensure you have the PyMongo module installed:

pip install pymongo

Example usage:

python geojson-mongo-import.py -f points.geojson -d geospatial -c points

Given an input file named "points.geojson" having GeoJSON points and destination collection named "points" in the "geospatial" database. This repo includes a sample "points.geojson" file which is a FeatureCollection of points and a couple of properties including a timestamp.

Additional parameters are required if your server is not on your local machine or if your database requires authentication. Use the "--help" flag or see the example below.

python geojson-mongo-import.py -f points.geojson -s host.example.com -d mydb -c points -u user -p password

This script uses bulk write operations which results in nearly a 10x performance boost. This performance improvement is very noticeable with large GeoJSON files.

Along with importing the GeoJSON file, this script also creates a MongoDB 2dsphere geospatial index on the collection (if the named index does not exist). It also creates the named database and collection if they do not exist (and the user has permission to do so).

Script requires MongoDB 3.2 or higher (prior releases do not support bulk operations), and was developed using Python 2.7 and a MongoDB 3.4 server.

geojson-mongo-import.py's People

Contributors

rtbigdata avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.