def handler(path): try: # Returns metadata for girder to save it on the file model metadata = {} metadata['type_'] = 'raster' dataset = gdal.Open(path) if dataset.GetSubDatasets(): raise CannotHandleError('Cannot handle this data type') try: with rasterio.open(path) as src: bounds = get_bounds(src) except RasterioIOError: raise CannotHandleError('Cannot handle this data type') crs = get_projection_as_proj4(dataset) metadata['crs'] = crs metadata['bands'] = dataset.RasterCount metadata['bandInfo'] = get_band_info(dataset) metadata['affine'] = list(dataset.GetGeoTransform()) metadata['width'] = dataset.RasterXSize metadata['height'] = dataset.RasterYSize metadata['driver'] = dataset.GetDriver().LongName metadata['nativeBounds'] = bounds metadata['bounds'] = from_bounds_to_geojson(bounds, crs) schema = RasterSchema() try: return schema.load(metadata) except ValidationError as e: raise CannotHandleError(e.messages) except AttributeError: raise CannotHandleError('Gdal could not open dataset')
def handler(path): try: # Returns metadata for girder to save it on the file model metadata = {} metadata['type_'] = 'vector' dataset = ogr.Open(path) if not dataset: raise CannotHandleError() metadata['driver'] = dataset.GetDriver().GetName() metadata['layers'] = dataset.GetLayerCount() metadata['layerInfo'] = get_layers(dataset) layerBounds = [Polygon.from_bounds(i['nativeBounds']['left'], i['nativeBounds']['bottom'], i['nativeBounds']['right'], i['nativeBounds']['top']) for i in metadata['layerInfo']] union = unary_union(layerBounds) layer = dataset.GetLayer(0) crs = layer.GetSpatialRef().ExportToProj4() bounds = {'left': union.bounds[0], 'right': union.bounds[2], 'bottom': union.bounds[1], 'top': union.bounds[3]} metadata['crs'] = crs metadata['nativeBounds'] = bounds metadata['type_'] = 'vector' metadata['bounds'] = from_bounds_to_geojson(bounds, crs) schema = VectorSchema() return schema.load(metadata) except AttributeError: raise CannotHandleError('Ogr could not open dataset')
def test_bad_crs_string(): nativeBounds = { 'left': 271785.000, 'bottom': 4345785.000, 'right': 506715.000, 'top': 4584315.000 } badBounds = from_bounds_to_geojson(nativeBounds, 'foobar') assert badBounds == ''
def get_layer_info(layer): metadata = {} extent = layer.GetExtent() crs = layer.GetSpatialRef().ExportToProj4() bounds = {'left': extent[0], 'right': extent[1], 'bottom': extent[2], 'top': extent[3]} metadata['crs'] = crs metadata['nativeBounds'] = bounds metadata['type_'] = 'vector' metadata['bounds'] = from_bounds_to_geojson(bounds, crs) metadata['featureCount'] = layer.GetFeatureCount() definition = layer.GetLayerDefn() count = definition.GetFieldCount() metadata['geomType'] = ogr.GeometryTypeToName(definition.GetGeomType()) metadata['layerFields'] = [get_field_info(definition.GetFieldDefn(i)) for i in range(count)] return metadata
def handler(path, girder_file): if ".obj" not in girder_file['name'] and \ ".OBJ" not in girder_file['name']: raise CannotHandleError(girder_file['name'] + ' is not an OBJ file') srs, offset = redOBJMeta(path) try: bounds = getOBJBounds(path, offset) geoJsonBounds = from_bounds_to_geojson(bounds, srs) geometa = { 'crs': srs, 'nativeBounds': bounds, 'bounds': geoJsonBounds, 'type_': 'vector', 'driver': 'OBJ' } schema = OBJSchema() return schema.load(geometa) except Exception: raise CannotHandleError('Failed to add geometa to OBJ file')
def get_subdataset_info(subDataset): dataset = gdal.Open(subDataset) wgs84 = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ' crs = get_projection_as_proj4(dataset) or wgs84 metadata = {} if dataset.GetGCPCount() > 0: bounds = get_bounds_from_gcp(dataset) else: bounds = get_bounds(dataset) metadata['crs'] = crs metadata['type_'] = 'grid' metadata['name'] = dataset.GetDescription() metadata['driver'] = dataset.GetDriver().LongName metadata['nativeBounds'] = bounds metadata['bounds'] = from_bounds_to_geojson(bounds, crs) metadata['affine'] = list(dataset.GetGeoTransform()) metadata['width'] = dataset.RasterXSize metadata['height'] = dataset.RasterYSize return metadata
def handler(path): try: # Returns metadata for girder to save it on the file model metadata = {} metadata['type_'] = 'grid' main_dataset = gdal.Open(path) try: gdal.Open(main_dataset.GetSubDatasets()[0][0]) except IndexError: raise CannotHandleError('Does not have subdatasets') dataset = gdal.Open(main_dataset.GetSubDatasets()[0][0]) wgs84 = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ' crs = get_projection_as_proj4(dataset) or wgs84 metadata = {} metadata['subDatasets'] = get_subdatasets(main_dataset) subDatasetBounds = [ Polygon.from_bounds(i['nativeBounds']['left'], i['nativeBounds']['bottom'], i['nativeBounds']['right'], i['nativeBounds']['top']) for i in metadata['subDatasets'] ] union = unary_union(subDatasetBounds) bounds = { 'left': union.bounds[0], 'right': union.bounds[2], 'bottom': union.bounds[1], 'top': union.bounds[3] } metadata['crs'] = crs metadata['type_'] = 'grid' metadata['driver'] = dataset.GetDriver().LongName metadata['nativeBounds'] = bounds metadata['bounds'] = from_bounds_to_geojson(bounds, crs) schema = GridSchema() return schema.load(metadata) except AttributeError: raise CannotHandleError('Gdal could not open dataset')
def sampleBounds(): crs = '+proj=utm +zone=10 +datum=WGS84 +units=m +no_defs ' return from_bounds_to_geojson(sampleNativeBounds(), crs)