def flood_hazard(overlay, flood_shp):
    """
    Create a tiled flood hazard raster
    """
    if flood_shp is None:
        return None
    else:
        # read flood shp
        shapes = gpd.read_file(flood_shp)
        flood_poly = [s for s in shapes.geometry if s is not None]

        # rasterize
        flood_raster = gps.rasterize(
            flood_poly, 4326, 12, 5, gps.CellType.INT8)

        # reclassify values to fit on 1-7 scale
        flood_reclass = flood_raster.reclassify(value_map={5: 5},
                                                data_type=int,
                                                replace_nodata_with=1,
                                                classification_strategy=gps.ClassificationStrategy.EXACT)

        # layout
        flood_tiled = flood_reclass.tile_to_layout(
            gps.GlobalLayout(zoom=12), 3857)

        # convert to an appropriate format for the overlay
        return _union_with_base(flood_tiled, overlay)
def storm_surge(overlay, storm_surge_tiff):
    """
    Create a tiled storm surge layer
    """

    # read in tiff
    storm_raster = gps.geotiff.get(gps.LayerType.SPATIAL,
                                   storm_surge_tiff,
                                   max_tile_size=128,
                                   num_partitions=32)

    # specify value map
    rmap = {}
    i = 2
    for x in range(1, 21, 3):
        rmap[x + 1] = int(i)
        i += 1

    # reclassify storm surge
    storm_reclass = storm_raster.reclassify(value_map=rmap,
                                            data_type=int,
                                            replace_nodata_with=1,
                                            classification_strategy=gps.ClassificationStrategy.GREATER_THAN_OR_EQUAL_TO)

    # convert to same layout as
    storm_tiled = storm_reclass.tile_to_layout(gps.GlobalLayout(zoom=12), 3857)
    return storm_tiled * overlay.base
Beispiel #3
0
def importdata(tiffpath, target_crs=None):
    spatial_raster_layer = gps.geotiff.get(layer_type=gps.LayerType.SPATIAL,
                                           uri=tiffpath)
    spatial_tiled_layer = spatial_raster_layer.tile_to_layout(
        layout=gps.GlobalLayout())
    if not target_crs:
        spatial_raster_layer.reproject(target_crs=4326).collect_metadata().crs

    lyr_name = os.path.basename(tiffpath).split(',')[0]
    gps.write(uri=uri_address,
              layer_name=lyr_name,
              tiled_raster_layer=spatial_tiled_layer)
Beispiel #4
0
    def test_key_to_extent(self):
        kt_layout = gps.KeyTransform(self.layout)
        self.assertEqual(gps.Extent(0.0, 0.8, 0.2, 1.0),
                         kt_layout.key_to_extent(gps.SpatialKey(0, 0)))

        kt_local = gps.KeyTransform(gps.LocalLayout(2),
                                    extent=gps.Extent(0, 0, 1, 1),
                                    dimensions=(10, 10))
        self.assertEqual(gps.Extent(0.0, 0.8, 0.2, 1.0),
                         kt_layout.key_to_extent(gps.SpatialKey(0, 0)))

        kt_global = gps.KeyTransform(gps.GlobalLayout(zoom=1), crs=4326)
        nw_global_extent = kt_global.key_to_extent(gps.SpatialKey(0, 0))
        self.assertTrue(
            abs(nw_global_extent.xmin + 180.0) <= 1e-4
            and abs(nw_global_extent.xmax) <= 1e-4
            and abs(nw_global_extent.ymin) <= 1e-4
            and abs(nw_global_extent.ymax - 90) <= 1e-4)
Beispiel #5
0
def map_ndvi(M, img, bounds, crs):
    # Start a spark context if needed
    init_sc()
    # Color ramp for NDVI
    ndvi_breaks_dict = {
        0.05: 0xffffe5aa,
        0.1: 0xf7fcb9ff,
        0.2: 0xd9f0a3ff,
        0.3: 0xaddd8eff,
        0.4: 0x78c679ff,
        0.5: 0x41ab5dff,
        0.6: 0x238443ff,
        0.7: 0x006837ff,
        1.0: 0x004529ff
    }
    ndvi_color_map = gps.ColorMap.from_break_map(ndvi_breaks_dict)

    # Convert the CRS into a proj4 string
    srs = osr.SpatialReference()
    srs.ImportFromWkt(crs.wkt)
    proj4 = srs.ExportToProj4()

    # Create the projected extent
    projected_extent = gps.ProjectedExtent(gps.Extent(bounds.left,
                                                      bounds.bottom,
                                                      bounds.right,
                                                      bounds.top),
                                           proj4=proj4)

    tiles = sc.parallelize([(projected_extent,
                             gps.Tile.from_numpy_array(img,
                                                       no_data_value=0.0))])
    raster_layer = gps.geotrellis.RasterLayer.from_numpy_rdd(
        gps.LayerType.SPATIAL, tiles)
    tiled_raster_layer = raster_layer.tile_to_layout(
        gps.GlobalLayout(),
        target_crs=3857,
        partition_strategy=gps.HashPartitionStrategy(40))
    pyramid = tiled_raster_layer.pyramid(
        resample_method=gps.ResampleMethod.BILINEAR)

    tms = gps.TMS.build(pyramid, ndvi_color_map)
    M.add_layer(TMSRasterData(tms), name="ndvi")
    def set_study_area(self, spatial_file):
        """
        Define study are attributes from an input shopefile or geojson

        Args:
            spatial_file (string): filepath for shapefile of geojson
                that defines the study area
        """
        # read in study area shp
        self.study_area['shapefile'] = spatial_file
        base_gpd = gpd.read_file(spatial_file)
        base_poly = base_gpd.geometry[0]

        # set the geom and bbox in wgs
        self.study_area['geom_wgs'] = [base_poly]
        bbox = list(base_poly.bounds)
        self.study_area['bbox_wgs'] = bbox

        # centroid
        self.study_area['centroid_wgs'] = {
            'x': np.mean(bbox[0::2]),
            'y': np.mean(bbox[1::2])
        }

        # reproject to web mercator
        if not base_gpd.crs == {'init': 'epsg:3857'}:
            base_poly_wm = base_gpd.to_crs({'init': 'epsg:3857'}).geometry[0]

        # set the geom and bbox in wm
        self.study_area['geom_wm'] = [base_poly_wm]
        self.study_area['bbox_wm'] = list(base_poly_wm.bounds)

        # tiled raster
        base_raster = gps.rasterize([base_poly], 4326, 14, 1,
                                    gps.CellType.INT8)
        base_tiled = base_raster.tile_to_layout(gps.GlobalLayout(zoom=12),
                                                3857)
        self.base = base_tiled
def sea_level_rise(overlay, gdb, feet=5):
    """
    Create a tiled sea level rise layer
    """
    if gdb is None:
        return None
    else:
        # open geodatabase
        driver = ogr.GetDriverByName("OpenFileGDB")
        g = driver.Open(gdb)

        # find appropriate layer name
        suffix = 'slr_{}ft'.format(str(feet))
        for x in range(g.GetLayerCount()):
            layer_name = g.GetLayerByIndex(x).GetName()
            if layer_name.endswith(suffix):
                break

        # read sea level rise polygons
        sea_poly = []
        with fiona.open(gdb, layer=layer_name) as source:
            slr_crs = source.crs['init']
            for p in source:
                sea_poly += MultiPolygon(shape(p['geometry']))

        # rasterize
        sea_raster = gps.rasterize(sea_poly, 4326, 12, 5, gps.CellType.INT8)

        # reclassify values to fit on 1-7 scale
        sea_reclass = sea_raster.reclassify(value_map={5: 5},
                                            data_type=int,
                                            replace_nodata_with=1,
                                            classification_strategy=gps.ClassificationStrategy.EXACT)

        # convert layout
        sea_tiled = sea_reclass.tile_to_layout(gps.GlobalLayout(zoom=12), 3857)

        return _union_with_base(sea_tiled, overlay)
Beispiel #8
0
def main(data_path, area_of_interest, outfile):
    # Create the SparkContext
    conf = gps.geopyspark_conf(appName="geodatafw", master="local[*]")
    sc = SparkContext(conf=conf)

    # Create raster_layer object from Sentinel 2 data
    raster_layer = get_raster_layer(sc, data_path)

    # Tile the rasters within the layer and reproject them to Web Mercator.
    tiled_layer = raster_layer.tile_to_layout(layout=gps.GlobalLayout(),
                                              target_crs=3857)

    # Mask the tiles within the layer with the area of interest
    masked = tiled_layer.mask(geometries=area_of_interest)

    # We will now pyramid the masked TiledRasterLayer so that we can use it in a TMS server later.
    # pyramided_mask = masked.pyramid()

    # Save each layer of the pyramid locally so that it can be accessed at a later time.
    #for pyramid in pyramided_mask.levels.values():
    gps.write(uri='file://%s' % outfile,
              layer_name='munsmo',
              tiled_raster_layer=masked)
Beispiel #9
0
from pyspark import SparkContext
from shapely.geometry import box


# Create the SparkContext
conf = gps.geopyspark_conf(appName="geopyspark-example", master="local[*]")
sc = SparkContext(conf=conf)

# Read in the NLCD tif that has been saved locally.
# This tif represents the state of Pennsylvania.
raster_layer = gps.geotiff.get(layer_type=gps.LayerType.SPATIAL,
                               uri='/tmp/NLCD2011_LC_Pennsylvania.tif',
                               num_partitions=100)

# Tile the rasters within the layer and reproject them to Web Mercator.
tiled_layer = raster_layer.tile_to_layout(layout=gps.GlobalLayout(), target_crs=3857)

# Creates a Polygon that covers roughly the north-west section of Philadelphia.
# This is the region that will be masked.
area_of_interest = box(-75.229225, 40.003686, -75.107345, 40.084375)

# Mask the tiles within the layer with the area of interest
masked = tiled_layer.mask(geometries=area_of_interest)

# We will now pyramid the masked TiledRasterLayer so that we can use it in a TMS server later.
pyramided_mask = masked.pyramid()

# Save each layer of the pyramid locally so that it can be accessed at a later time.
for pyramid in pyramided_mask.levels.values():
    gps.write(uri='file:///tmp/pa-nlcd-2011',
              layer_name='north-west-philly',
Beispiel #10
0
        'uri':
        's3://landsat-pds/L8/107/035/LC81070352015218LGN00/LC81070352015218LGN00_B9.TIF',
        'scene_id': 'LC81070352015218LGN00',
        'date': '2015218',
        'band': '9'
    }]
    rdd0 = sc.parallelize(csv_data)
    rdd1 = rdd0.flatMap(get_metadata)
    rdd1.first()

    rdd2 = rdd1.map(get_data)
    rdd2.first()

    rdd3 = rdd2.groupBy(lambda line: line['projected_extent'])
    rdd3.first()

    raster_layer = gps.RasterLayer.from_numpy_rdd(gps.LayerType.SPACETIME,
                                                  num_partitions=840,
                                                  rdd3)
    tiled_raster_layer = raster_layer.tile_to_layout(layout=gps.GlobalLayout(),
                                                     target_crs=3857)
    pyramid = tiled_raster_layer.pyramid()

    with open('SF_hex.csv', 'r') as SFgrids:
        coord = csv.reader(SFgrids)
        for layer in pyramid.levels.values(coord):
            gps.write("file:///tmp/values/",
                      "landsat",
                      layer,
                      time_unit=gps.TimeUnit.DAYS)