%%bash
wget https://www.dropbox.com/s/gzxe2ehk94rclnp/data.tar.gz?dl=0
tar xf data.tar.gz?dl=0
mv data* /tmp/
from osgeo import ogr, osr
from shapely import wkt
from shapely.geometry import GeometryCollection
import os
def reproject_layer(inLayer, outSpatialRef, target_ds, target_name):
inSpatialRef = inLayer.GetSpatialRef()
coordTrans = osr.CoordinateTransformation(inSpatialRef, outSpatialRef)
# create the output layer
if target_ds.GetLayer(target_name):
target_ds.DeleteLayer(target_name)
outLayer = target_ds.CreateLayer(
target_name,
outSpatialRef,
geom_type=inLayer.GetGeomType())
# add fields
inLayerDefn = inLayer.GetLayerDefn()
for i in range(inLayerDefn.GetFieldCount()):
fieldDefn = inLayerDefn.GetFieldDefn(i)
outLayer.CreateField(fieldDefn)
# get the output layer's feature definition
outLayerDefn = outLayer.GetLayerDefn()
# loop through the input features
inFeature = inLayer.GetNextFeature()
while inFeature:
# get the input geometry
geom = inFeature.GetGeometryRef()
# reproject the geometry
geom.Transform(coordTrans)
# create a new feature
outFeature = ogr.Feature(outLayerDefn)
# set the geometry and attribute
outFeature.SetGeometry(geom)
for i in range(0, outLayerDefn.GetFieldCount()):
outFeature.SetField(outLayerDefn.GetFieldDefn(i).GetNameRef(), inFeature.GetField(i))
# add the feature to the shapefile
outLayer.CreateFeature(outFeature)
# dereference the features and get the next input feature
outFeature = None
inFeature = inLayer.GetNextFeature()
def transform_geometry(a_geom, b_geom):
coordTrans = osr.CoordinateTransformation(
a_geom.GetSpatialReference(), b_geom.GetSpatialReference())
a_geom.Transform(coordTrans)
navajo_ds = ogr.Open('/tmp/data/navajo')
navajo_lyr = navajo_ds.GetLayer('cb_2017_us_aiannh_500k')
utah_ds = ogr.Open('/tmp/data/utah', 1)
utah_lyr = utah_ds.GetLayer('Utah')
navajo_feat = navajo_lyr.GetFeature(158)
navajo_feat.NAME
navajo_geom = navajo_feat.geometry().Clone()
utah_feat = utah_lyr.GetFeature(1)
utah_feat.State
utah_geom = utah_feat.geometry().Clone()
inSpatialRef = navajo_geom.GetSpatialReference()
outSpatialRef = utah_geom.GetSpatialReference()
coordTrans = osr.CoordinateTransformation(inSpatialRef, outSpatialRef)
navajo_geom.Transform(coordTrans)
navajo_geom.Intersection(utah_geom).GetArea()
e_ds = ogr.Open('/tmp/data/electric_power_lines')
e_lyr = e_ds.GetLayer('Electric_Power_Transmission_Lines')
reproject_layer(e_lyr, outSpatialRef, utah_ds, 'Electric_Power_Transmission_Lines')
del e_ds
del utah_ds
ds = ogr.Open('/tmp/data/utah')
utah = ds.GetLayer('Utah')
e_lyr = ds.GetLayer('Electric_Power_Transmission_Lines')
utah_feat = utah.GetFeature(1)
utah_geom = utah_feat.geometry().Clone()
utah_geom
GeometryCollection([wkt.loads(navajo_geom.ExportToWkt()), wkt.loads(utah_geom.ExportToWkt())])
inters = navajo_geom.Intersection(utah_geom)
wkt.loads(inters.ExportToWkt())
e_lyr.SetSpatialFilter(inters)
geometries = []
for f in e_lyr:
geometries.append(f.geometry().Clone())
lines = [wkt.loads(g.ExportToWkt()) for g in geometries]
GeometryCollection(lines)
len(geometries)
GeometryCollection(lines + [wkt.loads(inters.ExportToWkt())])
del navajo_ds
del ds