#NEWLINE# def main( self ) : #NEWLINE# #INDENT# source = ogr . Open ( self . args . input , False ) #NEWLINE# source_layer = source . GetLayer ( 0 ) #NEWLINE# try : #NEWLINE# #INDENT# shutil . rmtree ( self . args . output ) #NEWLINE# #UNINDENT# except OSError : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# driver = ogr . GetDriverByName ( 'ESRI Shapefile' ) #NEWLINE# dest = driver . CreateDataSource ( self . args . output ) #NEWLINE# dest_layer = dest . CreateLayer ( 'difference' , geom_type = ogr . wkbMultiPolygon ) #NEWLINE# for i in range ( source_layer . GetLayerDefn ( ) . GetFieldCount ( ) ) : #NEWLINE# #INDENT# dest_layer . CreateField ( source_layer . GetLayerDefn ( ) . GetFieldDefn ( i ) ) #NEWLINE# #UNINDENT# mask_features = [ ] #NEWLINE# mask_boxes = [ ] #NEWLINE# for mask in self . args . masks : #NEWLINE# #INDENT# geo = ogr . Open ( mask , False ) #NEWLINE# layer = geo . GetLayer ( 0 ) #NEWLINE# for feature in layer : #NEWLINE# #INDENT# mask_features . append ( feature ) #NEWLINE# mask_boxes . append ( get_bounding_box ( feature . GetGeometryRef ( ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# for feature in source_layer : #NEWLINE# #INDENT# masked_feature = ogr . Feature ( feature_def = source_layer . GetLayerDefn ( ) ) #NEWLINE# masked_feature . SetFrom ( feature ) #NEWLINE# masked_geometry = feature . GetGeometryRef ( ) . Clone ( ) #NEWLINE# for ( i , mask_feature ) in enumerate ( mask_features ) : #NEWLINE# #INDENT# bounding_box = mask_boxes [ i ] #NEWLINE# if ( not masked_geometry . Intersects ( bounding_box ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# masked_geometry = masked_geometry . Difference ( mask_feature . GetGeometryRef ( ) ) #NEWLINE# #UNINDENT# masked_feature . SetGeometryDirectly ( masked_geometry ) #NEWLINE# dest_layer . CreateFeature ( masked_feature )