def get_polygons_from_shapefile(image_filepath, input_shapefile_filepath): coor, gt, coor_system = get_coor_in_space(image_filepath) transform_mat = compute_epsg_to_image_mat(coor, gt) file = ogr.Open(input_shapefile_filepath) assert file is not None, "File {} does not exist!".format( input_shapefile_filepath) shape = file.GetLayer(0) feature_count = shape.GetFeatureCount() polygons = [] properties_list = [] for feature_index in range(feature_count): feature = shape.GetFeature(feature_index) raw_json = feature.ExportToJson() parsed_json = json.loads(raw_json) # Extract polygon: polygon = np.array(parsed_json["geometry"]["coordinates"][0]) polygon_epsg_space = polygon polygon_image_space = apply_transform_mat(polygon_epsg_space, transform_mat) polygon_image_space = polygon_utils.swap_coords(polygon_image_space) polygons.append(polygon_image_space) # Extract properties: if "properties" in parsed_json: properties = parsed_json["properties"] properties_list.append(properties) if properties_list: return polygons, properties_list else: return polygons
def create_ogr_polygon(polygon, transform_mat): polygon_swapped_coords = polygon_utils.swap_coords(polygon) polygon_epsg = apply_transform_mat(polygon_swapped_coords, transform_mat) ring = ogr.Geometry(ogr.wkbLinearRing) for coord in polygon_epsg: ring.AddPoint(coord[0], coord[1]) # Create polygon poly = ogr.Geometry(ogr.wkbPolygon) poly.AddGeometry(ring) return poly.ExportToWkt()
def get_polygons_from_osm(image_filepath, tag=""): coor, gt, coor_system = get_coor_in_space(image_filepath) transform_mat = compute_epsg_to_image_mat(coor, gt) osm_data = get_osm_data(coor[1]) polygons = [] for way in osm_data.ways: if way.tags.get(tag, "n/a") != 'n/a': # polygon = way.nodes[:-1] # Start and end vertex are the same so remove the end vertex polygon = way.nodes polygon_epsg_space = proj_to_epsg_space(polygon, coor_system) polygon_image_space = apply_transform_mat(polygon_epsg_space, transform_mat) polygon_image_space = polygon_utils.swap_coords(polygon_image_space) polygons.append(polygon_image_space) return polygons
def get_polygons_from_shapefile(image_filepath, input_shapefile_filepath): coor, gt, coor_system = get_coor_in_space(image_filepath) transform_mat = compute_epsg_to_image_mat(coor, gt) file = ogr.Open(input_shapefile_filepath) assert file is not None, "File {} does not exist!".format( input_shapefile_filepath) shape = file.GetLayer(0) feature_count = shape.GetFeatureCount() polygons = [] properties_list = [] for feature_index in range(feature_count): feature = shape.GetFeature(feature_index) raw_json = feature.ExportToJson() parsed_json = json.loads(raw_json) # Extract polygon: polygon = np.array(parsed_json["geometry"]["coordinates"][0]) assert len(polygon.shape) == 2, "polygon should have shape (n, d)" if 2 < polygon.shape[1]: print_utils.print_warning( "WARNING: polygon from shapefile has shape {}. Will discard extra values to have polygon with shape ({}, 2)" .format(polygon.shape, polygon.shape[0])) polygon = polygon[:, :2] polygon_epsg_space = polygon polygon_image_space = apply_transform_mat(polygon_epsg_space, transform_mat) polygon_image_space = polygon_utils.swap_coords(polygon_image_space) polygons.append(polygon_image_space) # Extract properties: if "properties" in parsed_json: properties = parsed_json["properties"] properties_list.append(properties) if properties_list: return polygons, properties_list else: return polygons