I have created a GeoJSON of linestrings in pixel space using Python. However, I now need to re-project the file to match the same projections and extent of the .tiff file I wish to layer the GeoJSON on top of. Is there a straightforward way to do this using rasterio, geopandas, or GDAL? I would rather not have to loop over the coordinates of every linestring since that is time consuming and computationally expensive. How should I go about this?
You could use shapely's affine_transform
:
import geopandas as gpd
import rasterio as rio
from shapely.affinity import affine_transform
infile = "/tmp/test.geojson"
outfile = "/tmp/out.geojson"
raster = "/tmp/test.tif"
df = gpd.read_file(infile)
with rio.open(raster) as ds:
t = ds.transform
df.geometry = df.geometry.apply(lambda geom: affine_transform(geom, [t.a,t.b,t.d,t.e,t.xoff,t.yoff]))
df.to_file(outfile, driver='GeoJSON')