Loading Spatial Data

Now we are ready to import some spatial data into the database. There are many means and formats which one can use to import data into the database. We will be looking at converting well-know SHP files into GeoJSON and then importing the GeoJSON file into the database.

Download Spatial Data

In the even that you do not have data to work with yet, you can take a look at OpenStreetMap Data Extracts. OpenStreetMap is an open source version of Google Maps. It has geographical data from all over the world.

Navigate to any country and download the “.shp.zip ” file for that country.

Once the file is done downloading, Unzip it to get a list of strange looking files:

Convert to GeoJSON

The next step is to convert these files to a GeoJSON file. This is done by using the tool osg2osg:

ogr2ogr -f GeoJSON -t_srs crs:84 file_name.geojson file_name.shp
  • -f GeoJSON specifies the format which to convert the shape file to.
  • -t srs crs:84 ensures that the proper format is used to conver7the shape file
  • file_name.geojson is the file which should be created and can be named anything
  • file_name.shp is the name of the shape file which you have downloaded

Import Into MongoDB

After converting the shape file, there should be a name.geojson file in the same folder. This file is now ready to be uploaded/imported into the mongo Database. We do this by using the mongoimport tool which was downloaded together with all the tools when you downloaded the database.

The following command enters this geojson data into the database you created:

mongoimport --db SomeDatabase -c SomeCollection --file "file_name.geojson" --jsonArray
  • –db SomeDatabase specifies the database which will be created or used
  • -c SomeCollection creates or uses a collection by the name of “SomeCollection”
  • –file specifies the file which is already in the geojson format
  • –jsonArray means that the features are already in a json array []

Leave a comment

Design a site like this with WordPress.com
Get started