Skip to content

Instantly share code, notes, and snippets.

View GerardoLopez's full-sized avatar

Gerardo López-Saldaña GerardoLopez

View GitHub Profile
import os
import gdal, ogr, osr
import numpy as np
def createBuffer(inputfn, outputBufferfn, bufferDist, srs):
inputds = ogr.Open(inputfn)
inputlyr = inputds.GetLayer()
shpdriver = ogr.GetDriverByName('ESRI Shapefile')
@GerardoLopez
GerardoLopez / mosaic.py
Created March 25, 2020 17:30
Creates a mosaic from some MODIS tiles for all SubDataset for a specific product
# Creates a mosaic for the UK for a defined bounding box:
# - for all Days of Year (DoY) in julian day format for a specific year
# - using the tiles specified
# - for the selected product and each associated subdataset
import os
import gdal
import subprocess
from glob import glob
@GerardoLopez
GerardoLopez / reproject_when_opening.py
Created March 26, 2020 11:41
Lazy in-memory reprojection to a user-specified Coordinate Reference System (CRS)
import xarray as xr
import rasterio
from rasterio.vrt import WarpedVRT
fname = '/data/peatlands_test_data/MOD11A1/LST_Day_1km/h17v03/MOD11A1_LST_Day_1km_h17v03_2019-08.tif'
# Lazy in-memory warping from Sinusoidal to WGS84 lat/lon
with rasterio.open(fname) as src:
# print(src.profile)
with WarpedVRT(src, crs='epsg:4326') as vrt:
@GerardoLopez
GerardoLopez / transform.py
Created March 27, 2020 13:25
Transforms coordinates between different Spatial Reference System (SRS)
# Transforms from one Spatial Reference System (SRS) to another one
# the SRSs must be define from their PROJ4 or WKT strings
import osr
# Sinusoidal definition
# from https://spatialreference.org/ref/sr-org/6842/
# It fully match with the metadata in the MODIS products
sinusoidal_srs = (f'+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 '
f'+b=6371007.181 +units=m +no_defs ')
@GerardoLopez
GerardoLopez / postgres_queries_and_commands.sql
Created September 28, 2020 12:54 — forked from rgreenjr/postgres_queries_and_commands.sql
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT procpid, age(clock_timestamp(), query_start), usename, current_query
FROM pg_stat_activity
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;
-- show running queries (9.2)
SELECT pid, age(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
@GerardoLopez
GerardoLopez / save_xarray_to_gtiff.py
Created February 23, 2021 11:04
Save xarray to a Cloud Optimized GeoTIFF (COG)
import gdal
from osgeo import gdal_array
import osr
import numpy
def get_dst_dataset(dst_img, cols, rows, layers, dtype, proj, gt):
"""
Create a GDAL data set in Cloud Optimized GeoTIFF (COG) format
:param dst_img: Output filenane full path
@GerardoLopez
GerardoLopez / OverlayRasterOnIPyLeaflet.ipynb
Created August 9, 2021 16:15
Notebook showing the use of xarray_leaflet (https://github.com/davidbrochart/xarray_leaflet) to overlay xarrays into a IPyLeaflet map. Use the gls.txt conda env to run the Notebook.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@GerardoLopez
GerardoLopez / EOCIS_QuickStartGuide_example.ipynb
Created May 26, 2023 09:06
EOCIS Quick Start Guide example
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@GerardoLopez
GerardoLopez / getAttributesFromntersectingFeatures.py
Created February 23, 2024 12:33
Get attributes from features in one layer that intersect features from another layer
from osgeo import ogr
def ogrIntersection(tiles_layer, site_fname):
# Get layer for the site
driver = ogr.GetDriverByName("GeoJSON")
src_GeoJSON = driver.Open(site_fname)
site_layer = src_GeoJSON.GetLayer()
# List with tiles for every feature in site GeoJSON