Skip to content

Instantly share code, notes, and snippets.

@esgn
Created February 16, 2021 14:18
Show Gist options
  • Save esgn/a9a334bc54e2bb8f07ca8e081f9fc9ec to your computer and use it in GitHub Desktop.
Save esgn/a9a334bc54e2bb8f07ca8e081f9fc9ec to your computer and use it in GitHub Desktop.
from urllib.request import urlretrieve
import rasterio
import pyproj
from shapely.geometry import box
from shapely.ops import transform
from shapely.geometry import shape, GeometryCollection
import sys
import os
import json
import tarfile
# get rough boundary
paris = "https://france-geojson.gregoiredavid.fr/repo/departements/75-paris/departement-75-paris.geojson"
urlretrieve(paris, 'paris.geojson')
wgs84 = pyproj.CRS('EPSG:4326')
lamb93 = pyproj.CRS('EPSG:2154')
project = pyproj.Transformer.from_crs(lamb93, wgs84, always_xy=True).transform
with open("paris.geojson") as f:
gjs = json.load(f)['geometry']
paris = shape(gjs)
tiff_files = []
# Get TIFF inside polygon
for f in os.listdir('.'):
if f.endswith('.TIF'):
src = rasterio.open(f)
bounds = src.bounds
bbox = box(*bounds)
bbox = transform(project, bbox)
if (bbox.intersects(paris)):
tiff_files.append(f.replace(".TIF",""))
# Put TIFF and other relevant files in archive
tar = tarfile.open("paris.tar.gz","w:gz")
for t in tiff_files:
tar.add(t+".TIF")
tar.add(t+".TFW")
tar.add(t+".HDR")
print(t + " added to tar file")
tar.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment