def CreateNewShapeLayer(shapeName):
driver = ogr.GetDriverByName("ESRI Shapefile")
data_source = driver.CreateDataSource(shapeName)
# create the spatial reference, WGS84
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
# create the layer
layer = data_source.CreateLayer(shapeName.rsplit("/", 1)[-1], srs, ogr.wkbLineString)
print ("Created layer {}".format(shapeName))
return layer
def UnionBlockCentroidStreetLines():
censusstreetlayersrc = "/Users/cthomas/Development/Data/spatial/Network/streets/tl_2016_06000_roads_la_clipped.shp"
censusStreets = ogr.Open(censusstreetlayersrc, 0)
censuslayer = censusStreets.GetLayer()
connectorlayersrc = "/Users/cthomas/Development/Data/spatial/Network/streets/street_segment_block_centroid_connectors.csv"
# Convert my CSV connector layer to a Shapefile
CreateShapeFromCSV(connectorlayersrc)
connectorStreets = ogr.Open(censusstreetlayersrcconnectorlayersrc.replace(".csv", ".shp"), 0)
connectorLayer = connectorStreets.GetLayer()
# Create a new empty shapefile to receive the results of my union
outputLayer = CreateNewShapeLayer(censusstreetlayersrc.rsplit("/", 1)[0] + "/la_streets_with_block_centroid_connectors.shp")
print("About to union the layers")
censuslayer.Union(connectorLayer, outputLayer)
outputLayer = None
censuslayer = None
connectorLayer = None
UnionBlockCentroidStreetLines()