Skip to content

Instantly share code, notes, and snippets.

@joshuapowell
Last active March 9, 2021 20:07
Show Gist options
  • Save joshuapowell/1fb8f9feff0a3e607c8c to your computer and use it in GitHub Desktop.
Save joshuapowell/1fb8f9feff0a3e607c8c to your computer and use it in GitHub Desktop.
Import Millions of Features Into Mapbox

Import Millions of Features Into Mapbox

This tutorial assumes you have either a SHP file or GeoJSON file with your data ready to go and project at WGS1984 (i.e., 4326). It also assumes you are operating under a Unix environment.

Prerequisites

  1. OGR
  2. Brew
  3. Tippecanoe
  4. Mapbox.com Account
  5. Mapbox Studio

Convert your Shapefile (SHP) to GeoJSON

Unzip and change into the Shapefile directory into terminal

ogr2ogr -f "GeoJSON" my_file.geojson my_file.shp

Convert your GeoJSON to MBTILES

tippecanoe -r 1.1 -o my_file.mbtiles my_file.geojson

Finally, you'll want to login to your Mapbox.com account, visit the Data page, and upload your newly created my_file.mbtiles file.

===

Work in Progress:

Alternative: CSV to GeoJSON

Alternatively, you can accomplish the same results by converting a CSV to GeoJSON if you do not have a Shapefile.

You will need to install Mapbox's csv2geojson tool

Once installed you will be able to run the folowing command:

csv2geojson --lat='Latitude' --lon='Longitude'  data.csv > data.geojson

For files larger than a few thousand lines, you will need to convert the CSV into a Shapefile with ogr2ogr

ogr2ogr -f "Esri Shapefile" ./ ./data.csv

This will generate a .dbf file. You will then need to create a new VRT file to convert the .dbf into the final Shapefile.

Create a file called dataSource.vrt with the following content:

<OGRVRTDataSource>
  <OGRVRTLayer name="mylayer1">
    <SrcDataSource relativeToVRT="1">/my_dir</SrcDataSource>
    <SrcLayer>sample1</SrcLayer>
    <GeometryType>wkbPoint</GeometryType>
    <LayerSRS>WGS84</LayerSRS>
    <GeometryField encoding="PointFromColumns" x="long" y="lat"/>
  </OGRVRTLayer>
</OGRVRTDataSource>

After you have the .dbf and .vrt file available, you can convert the results into your final Shapefile.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment