Skip to content

Instantly share code, notes, and snippets.

View davidheyman's full-sized avatar

David Heyman davidheyman

View GitHub Profile
@davidheyman
davidheyman / get_tile_size.py
Created November 12, 2018 08:24
Get tile sizes from style-optimised vector tiles on Mapbox
import mercantile
import requests
import settings
def getStyle():
r = requests.get('https://api.mapbox.com/styles/v1/axismaps/' + settings.MAPBOX_STYLE + '?access_token=' + settings.MAPBOX_TOKEN)
return r.json()['modified']
def download_tile(tileurl, tile, modified):
url = tileurl + '/{z}/{x}/{y}.mvt'.format(**tile.__dict__)
@davidheyman
davidheyman / get_tiles.py
Created November 12, 2018 07:23 — forked from perrygeo/get_tiles.py
Get all map tiles for a geographic extent and zoom level(s)
from __future__ import print_function
import mercantile
import urllib
import time
import os
def download_tile(tileurl, tile, output_dir, pause=1):
url = tileurl + '/{z}/{x}/{y}.png'.format(**tile.__dict__)
local_path = url.replace(tileurl, output_dir)
@davidheyman
davidheyman / prep_rasters.py
Created September 24, 2018 09:13
A script to clumsily deal with GeoTIFF transparency and data types and convert to MBTiles
"""Deal with raster transparency and convert to MBTiles"""
import os
import re
from string import Template
from shutil import copyfile
from osgeo import gdal
import rasterio
PATH = 'data/geotiff/'
"""Splits rasters into chunks and creates contours"""
import os
import argparse
import uuid
import re
from osgeo import gdal
def chunk():
"""Splits rasters into 5000 x 5000 chunks."""
# Install Mapshaper
npm install -g mapshaper
# Install Tippecanoe
brew install tippecanoe
# Make a directory for your geojson files and convert all the shapefiles in a directory to geojson
mkdir ../geojson
for f in *.shp;
do
@davidheyman
davidheyman / mapshaper.sh
Last active February 1, 2017 14:21
MapMaster Data Processing Commands
# Convert shp to topjson and simplify
mapshaper -i /Volumes/Storage/Box\ Sync/MapMaster\ data/World/Physical\ Environment/Vector/005_Water_Resources_Vulnerability/005_Water_Resources_Vulnerability.shp -simplify 5% -o data/world/005_water_resources.json format=topojson
# Load shapefiles in subdirectories into PostGIS
for f in *; do cd $f; for g in *.shp; do shp2pgsql -I -s 4326 $g `basename $g .shp` | psql -d mapmaster; cd ../; done; done
# create transparency band
for f in *tif;
do
gdal_translate -b 1 -ot Byte $f red.tif;
gdal_translate -b 2 -ot Byte $f green.tif;
gdal_translate -b 3 -ot Byte $f blue.tif;
gdal_translate -a_nodata none $f no.tif;
gdal_calc.py -A no.tif --outfile=mask.tif --calc="(A<=255)*255";
gdal_merge.py -separate -o ../ToCompress/$f red.tif green.tif blue.tif mask.tif;
sudo rm red.tif;
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y postgis postgresql-9.3-postgis-2.1 imagemagick
cd ~rails
git clone https://github.com/axismaps/cognita.git
cp ~rails/rails_project/config/database.yml ~rails/cognita/config/
DROP VIEW IF EXISTS bivariate;
CREATE VIEW bivariate AS
SELECT gid, gdp, pop, aal_gdp, aal_pop, aal_gdp / gdp AS gdp_pct, aal_pop / pop AS pop_pct, geom
FROM admin1
INNER JOIN (
SELECT aal AS aal_gdp, wb_adm1_co
FROM flood
WHERE analysis_sector = 'GDP' AND year = 2010
) AS fl ON admin1.adm1_code = fl.wb_adm1_co
INNER JOIN (

Keybase proof

I hereby claim:

  • I am davidheyman on github.
  • I am davidheyman (https://keybase.io/davidheyman) on keybase.
  • I have a public key whose fingerprint is E39B FE40 D15B 962B 189E 8C5C F0F2 DE54 5DED 409D

To claim this, I am signing this object: