def process_geojson_features(geojson_features, raster, header, csvfile):
    """Using a raster input, get values of a raster at features in geojson.
    Output the final points and depths to a csv file."""

    # write to csv file as depth found
    csv_writer = csv.writer(csvfile)
    csv_writer.writerow(header)

    # provide progress whilst processing: number given is the total number of features in the cruise track
    number_features_to_process = len(geojson_features)
    progress_report = ProgressReport(number_features_to_process)

    # process each of the shapefiles
    for feature in geojson_features:

        # get point values from raster at points in shapefile
        result = rasterstats.gen_point_query(feature, raster, geojson_out=True)

        # for each value obtained from the raster, output a line into a csvfile
        for r in result:
            csv_writer.writerow([
                r['properties']['date_time'], r.geometry.coordinates[1],
                r.geometry.coordinates[0], r['properties']['value']
            ])

            progress_report.increment_and_print_if_needed()
Esempio n. 2
0
def pointquery(features, raster, band, indent, nodata, interpolate, property_name, sequence, use_rs):
    """
    Queries the raster values at the points of the input GeoJSON Features.
    The raster values are added to the features properties and output as GeoJSON
    Feature Collection.

    If the Features are Points, the point geometery is used.
    For other Feauture types, all of the verticies of the geometry will be queried.
    For example, you can provide a linestring and get the profile along the line
    if the verticies are spaced properly.

    You can use either bilinear (default) or nearest neighbor interpolation.
    """

    results = gen_point_query(
        features,
        raster,
        band=band,
        nodata=nodata,
        interpolate=interpolate,
        property_name=property_name,
        geojson_out=True,
    )

    if sequence:
        for feature in results:
            if use_rs:
                click.echo(b"\x1e", nl=False)
            click.echo(json.dumps(feature))
    else:
        click.echo(json.dumps({"type": "FeatureCollection", "features": list(results)}))
Esempio n. 3
0
def pointquery(features, raster, band, indent, nodata, interpolate,
               property_name, sequence, use_rs):
    """
    Queries the raster values at the points of the input GeoJSON Features.
    The raster values are added to the features properties and output as GeoJSON
    Feature Collection.

    If the Features are Points, the point geometery is used.
    For other Feauture types, all of the verticies of the geometry will be queried.
    For example, you can provide a linestring and get the profile along the line
    if the verticies are spaced properly.

    You can use either bilinear (default) or nearest neighbor interpolation.
    """

    results = gen_point_query(features,
                              raster,
                              band=band,
                              nodata=nodata,
                              interpolate=interpolate,
                              property_name=property_name,
                              geojson_out=True)

    if sequence:
        for feature in results:
            if use_rs:
                click.echo(b'\x1e', nl=False)
            click.echo(json.dumps(feature))
    else:
        click.echo(
            json.dumps({
                'type': 'FeatureCollection',
                'features': list(results)
            }))
Esempio n. 4
0
def extract_elev(shapefile, route, rasterfile):
    da = read_shape(shapefile, route)
    raw_elev = pd.DataFrame(rasterstats.gen_point_query(da, rasterfile))
    elev = [x for x in raw_elev.iloc[0,:] if x is not None] 
    elev_meter = np.array(elev) * 0.3048
    
    return(elev_meter)
Esempio n. 5
0
def _extract_longitudes(lon_lines, lon_array, affine):
    for line in lon_lines:
        out_line_coords = []
        longitude = line["properties"]["dd"]
        for x, y, in shape(line["geometry"]).coords:
            point = Point(x, y)
            rastervalue = rasterstats.gen_point_query(
                point, lon_array, affine=affine).next()
            if rastervalue is None:
                continue

            if not out_line_coords:
                out_line_coords.append(point)
                if rastervalue >= 0.:
                    current_is_positive = True
                else:
                    current_is_positive = False

            if rastervalue >= 0.:
                if current_is_positive:
                    out_line_coords.append(point)
                else:
                    out_line_coords.append(point)
                    out_line = line
                    out_line.update(
                        geometry=mapping(LineString(out_line_coords)))
                    # longitude = -longitude
                    lon_int = int(round(-longitude))
                    direction = "W" if lon_int > 0 else "E"
                    display_lon = lon_int if lon_int > 0 else -lon_int
                    display = "%s %s" % (display_lon, direction)
                    if lon_int == 0:
                        direction = None
                        display = "0"
                    out_line["properties"].update(
                        dd=-longitude, degrees=lon_int, direction=direction,
                        display=display, scalerank=None)
                    out_line_coords = []
                    yield out_line
            elif rastervalue < 0.:
                if current_is_positive:
                    out_line_coords.append(point)
                    out_line = line
                    out_line.update(
                        geometry=mapping(LineString(out_line_coords)))
                    lon_int = int(round(longitude))
                    direction = "W" if lon_int > 0 else "E"
                    display_lon = lon_int if lon_int > 0 else -lon_int
                    display = "%s %s" % (display_lon, direction)
                    if lon_int == 0:
                        direction = None
                        display = "0"
                    out_line["properties"].update(
                        dd=longitude, degrees=lon_int, direction=direction,
                        display=display, scalerank=None)
                    out_line_coords = []
                    yield out_line
                else:
                    out_line_coords.append(point)

        if out_line_coords:
            if not current_is_positive:
                longitude = -longitude
            out_line = line
            out_line.update(
                geometry=mapping(LineString(out_line_coords)))
            lon_int = int(round(longitude))
            direction = "W" if lon_int > 0 else "E"
            display_lon = lon_int if lon_int > 0 else -lon_int
            display = "%s %s" % (display_lon, direction)
            if lon_int == 0:
                direction = None
                display = "0"
            out_line["properties"].update(
                dd=longitude, degrees=lon_int, direction=direction,
                display=display, scalerank=None)
            out_line_coords = []
            yield out_line