Пример #1
0
#with fiona.open(canal_path) as canal_src:
#    bounds = canal_src.bounds

grid_extent = fiona.open(path + "/grid_extent.geojson")
grid_feature = grid_extent[0]
grid_shape = shape(grid_feature['geometry'])
bounds = grid_shape.bounds

rv_array, affine = rasterize(canal_path, pixel_size=pixel_size, bounds=bounds)

# binary_raster_path = "/sciclone/aiddata10/REU/projects/afghanistan_gie/distance_to_canals/binary_canals.tif"
# binary_raster_path = "/sciclone/aiddata10/REU/projects/afghanistan_gie/distance_to_canals/binary_starts.tif"
binary_raster_path = path + "/binary_starts.tif"

export_raster(rv_array, affine, binary_raster_path)

# import tarfile

# def make_tarfile(dst, src):
#     with tarfile.open(dst, "w:gz") as tar:
#         tar.add(src, arcname=os.path.basename(src))

# make_tarfile(dst=binary_raster_path + ".tar.gz" , src=binary_raster_path)

# -----------------------------------------------------------------------------

# distance_raster_path = "/sciclone/aiddata10/REU/projects/afghanistan_gie/distance_to_canals/distance_canals.tif"
# distance_raster_path = "/sciclone/aiddata10/REU/projects/afghanistan_gie/distance_to_canals/distance_starts.tif"
distance_raster_path = path + "/distance_starts.tif"
affine = Affine(pixel_size, 0, xmin, 0, -pixel_size, ymax)

shape = (int((ymax - ymin) / pixel_size), int((xmax - xmin) / pixel_size))

shorelines, _ = rasterize(shorelines_path, affine=affine, shape=shape)
shorelines = np.logical_not(shorelines).astype(int)

lakes, _ = rasterize(lakes_path, affine=affine, shape=shape)
rivers, _ = rasterize(rivers_path, affine=affine, shape=shape)

water = shorelines + lakes + rivers

water_output_raster_path = "water_binary.tif"

export_raster(water, affine, water_output_raster_path)

# -----------------------------------------------------------------------------

# import rasterio
# water_src = rasterio.open(water_output_raster_path)
# water = water_src.read()[0]
# affine = water_src.affine

distance_output_raster_path = "water_distance.tif"


def raster_conditional(rarray):
    return (rarray == 1)

xmin = -180
xmax = 180
ymin = -90
ymax = 90

affine = Affine(pixel_size, 0, xmin, 0, -pixel_size, ymax)

shape = (int((ymax - ymin) / pixel_size), int((xmax - xmin) / pixel_size))

print "Rasterizing"
shorelines, _ = rasterize(shorelines_path, affine=affine, shape=shape)
shorelines = np.logical_not(shorelines).astype(int)

water_output_raster_path = "/sciclone/aiddata10/REU/geo/data/rasters/distance_to/coast_236/binary/coast_binary.tif"

export_raster(shorelines, affine, water_output_raster_path)

# -----------------------------------------------------------------------------

# import rasterio
# water_src = rasterio.open(water_output_raster_path)
# water = water_src.read()[0]
# affine = water_src.affine

distance_output_raster_path = "/sciclone/aiddata10/REU/geo/data/rasters/distance_to/coast_236/coast_distance.tif"

print "Generating distance raster"


def raster_conditional(rarray):
    return (rarray == 1)
Пример #4
0
for f in src_files:
    i += 1
    print "Processing: ", f

    features = fiona.open(f)

    rv_array, _ = rasterize(vectors=features, pixel_size=pixel_size,
                            affine=affine, shape=shape)

    output += rv_array * i

    # any cell with value > i must have multiple features
    output = np.where(output > i, mixed_val, output)


export_raster(output, affine=affine, nodata=255, path=categorical_output_raster_path)



# ------------------------
# distance raster

print "Generating distance raster"

def raster_conditional(rarray):
    return (rarray > 0)

dist = build_distance_array(output, affine=affine,
                            output=distance_output_raster_path,
                            conditional=raster_conditional)
ymin = -90
ymax = 90

affine = Affine(pixel_size, 0, xmin,
                0, -pixel_size, ymax)


shape = (int((ymax-ymin)/pixel_size), int((xmax-xmin)/pixel_size))

borders, _ = rasterize(borders_path, affine=affine, shape=shape)



borders_output_raster_path = "/sciclone/aiddata10/REU/geo/data/rasters/distance_to/gadm28_borders/binary/gadm28_borders_binary.tif"

export_raster(borders, affine, borders_output_raster_path)


# -----------------------------------------------------------------------------

# import rasterio
# borders_src = rasterio.open(borders_output_raster_path)
# borders = borders_src.read()[0]
# affine = borders_src.affine

distance_output_raster_path = "/sciclone/aiddata10/REU/geo/data/rasters/distance_to/gadm28_borders/gadm28_borders_distance.tif"


def raster_conditional(rarray):
    return (rarray == 1)
Пример #6
0
    # if r == 200:
    #     print "Run time: {0} seconds for {1} rows ({2}s avg)".format(row_dur, row_count, row_dur/row_count)

    #     print "t1 total: {0}, count: {1}, avg: {2}".format(t1, t1c, t1/t1c)
    #     print "t11 total: {0}, count: {1}, avg: {2}".format(t11, t11c, t11/t11c)
    #     print "t111 total: {0}, count: {1}, avg: {2}".format(t111, t111c, t111/t111c)

    #     print "t2 total: {0}, count: {1}, avg: {2}".format(t2, t2c, t2/t2c)
    #     print "t22 total: {0}, count: {1}, avg: {2}".format(t22, t22c, t22/t22c)
    #     print "t3 total: {0}, count: {1}, avg: {2}".format(t3, t3c, t3/t3c)

    #     raise

# print rv_array
# print z

print rv_array.shape
print nrows * ncols

print "t1 total: {0}, count: {1}, avg: {2}".format(t1, t1c, t1 / t1c)
print "t111 total: {0}, count: {1}, avg: {2}".format(t111, t111c, t111 / t111c)

print "t2 total: {0}, count: {1}, avg: {2}".format(t2, t2c, t2 / t2c)
print "t22 total: {0}, count: {1}, avg: {2}".format(t22, t22c, t22 / t22c)
print "t3 total: {0}, count: {1}, avg: {2}".format(t3, t3c, t3 / t3c)

dur = time.time() - t_start
print "Run time: {0} seconds".format(round(dur, 2))

export_raster(z, affine, output_raster_path)
                0, -pixel_size, ymax)

shape = (int((ymax-ymin)/pixel_size), int((xmax-xmin)/pixel_size))

roads = np.zeros(shape, dtype='byte')

for n in names:
    path = "{0}/{1}".format("/sciclone/aiddata10/REU/geo/raw/groads",
                            "groads-v1-{0}-shp/gROADS-v1-{0}.shp".format(n))
    rv_array, _ = rasterize(path, affine=affine, shape=shape)
    roads = roads | rv_array


roads_output_raster_path = "/sciclone/aiddata10/REU/geo/data/rasters/distance_to/groads/binary/groads_binary.tif"

export_raster(roads, affine, roads_output_raster_path)


# -----------------------------------------------------------------------------

# import rasterio
# roads_src = rasterio.open(roads_output_raster_path)
# roads = roads_src.read()[0]
# affine = roads_src.affine

distance_output_raster_path = "/sciclone/aiddata10/REU/geo/data/rasters/distance_to/groads/groads_distance.tif"


def raster_conditional(rarray):
    return (rarray == 1)
Пример #8
0
def mk_proj(inhdf, outdir):

    # get data time stamp
    datestring = inhdf.split(".")[1][1:]
    newtime = get_time(datestring)

    # get new data export name
    dayout = os.path.join(outdir, "modis_lst_day_cmg_" + newtime + ".tif")
    nightout = os.path.join(outdir, "modis_lst_night_cmg_" + newtime + ".tif")

    hdf = SD(inhdf, SDC.READ)

    # Read dataset.
    data2D_day = hdf.select('LST_Day_CMG')
    data_day = data2D_day[:, :].astype(np.float64)

    data2D_night = hdf.select('LST_Night_CMG')
    data_night = data2D_night[:, :].astype(np.float64)

    # Read global attribute.
    fattrs = hdf.attributes(full=1)
    ga = fattrs["StructMetadata.0"]
    gridmeta = ga[0]

    # Construct the grid.  The needed information is in a global attribute
    # called 'StructMetadata.0'.  Use regular expressions to tease out the
    # extents of the grid.

    ul_regex = re.compile(
        r'''UpperLeftPointMtrs=\(
                                      (?P<upper_left_x>[+-]?\d+\.\d+)
                                      ,
                                      (?P<upper_left_y>[+-]?\d+\.\d+)
                                      \)''', re.VERBOSE)
    match = ul_regex.search(gridmeta)
    x0 = np.float(match.group('upper_left_x'))
    y0 = np.float(match.group('upper_left_y'))

    lr_regex = re.compile(
        r'''LowerRightMtrs=\(
                                      (?P<lower_right_x>[+-]?\d+\.\d+)
                                      ,
                                      (?P<lower_right_y>[+-]?\d+\.\d+)
                                      \)''', re.VERBOSE)
    match = lr_regex.search(gridmeta)

    x1 = np.float(match.group('lower_right_x'))
    y1 = np.float(match.group('lower_right_y'))
    ny, nx = data_day.shape
    xinc = (x1 - x0) / nx
    yinc = (y1 - y0) / ny

    x = np.linspace(x0, x0 + xinc * nx, nx)
    y = np.linspace(y0, y0 + yinc * ny, ny)
    xv, yv = np.meshgrid(x, y)

    # In basemap, the sinusoidal projection is global, so we won't use it.
    # Instead we'll convert the grid back to lat/lons.
    #sinu = pyproj.Proj("+proj=sinu +R=6371007.181 +nadgrids=@null +wktext")
    #+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
    #+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs
    sinu = pyproj.Proj(
        "+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
    )
    wgs84 = pyproj.Proj("+proj=longlat +datum=WGS84 +no_defs")
    lon, lat = pyproj.transform(sinu, wgs84, xv, yv)

    xmin = lon[-1, 0]
    ymax = lat[0, -1]

    pixel_size = 0.05
    affine = Affine(pixel_size, 0, xmin, 0, -pixel_size, ymax)

    data_day = data_day * scale_factor
    data_night = data_night * scale_factor

    distancerasters.export_raster(data_day, affine, dayout, nodata=0)
    distancerasters.export_raster(data_night, affine, nightout, nodata=0)
        # build intermediary rasters for individual categories/years

        print "selected {0} features for field: {1}, and year: {2}".format(
            len(features_filtered), cat, year)

        if len(features_filtered) == 0:
            print "\tno feature selected for violence type {0} and year {1}".format(
                cat, year)
            pass

        else:

            cat_raster, _ = rasterize(features_filtered,
                                      affine=affine,
                                      shape=out_shape)

        output_file = "ucdp_" + field_values[cat] + "_" + str(year) + ".tif"
        output_path = os.path.join(final_output, output_file)

        export_raster(cat_raster,
                      affine=affine,
                      path=output_path,
                      out_dtype='float64',
                      nodata=None)

t2 = time.time()

sum_time = t2 - t1

print sum_time