Пример #1
0
 def __serialize__(self):
     return {
         'id': self.id,
         'IMEI': self.imei,
         'location': dumps(Feature(to_shape(self.location))),
         'height': self.height,
         'deployed': self.deployed,
         'retrieved': self.retrieved,
         'url': self.url
     }
Пример #2
0
def convert(wkt):
    utm = loads(wkt)
    if isinstance(utm, LineString):
        latlon_2d = LineString(
            [utm_to_latlon(xy[0], xy[1]) for xy in list(utm.coords)])
    elif isinstance(utm, Point):
        latlon_2d = Point(
            [utm_to_latlon(xy[0], xy[1]) for xy in list(utm.coords)][0])
    else:
        raise Exception("unknown geometry: %s" % type(utm))
    return json.loads(dumps(latlon_2d))
Пример #3
0
def build_order_from_metadata(metadata_df,
                              idx,
                              row,
                              products_bundles,
                              sufix='sample_',
                              partial=False):

    sample_id = idx

    filtered_df = metadata_df[metadata_df.sample_id == sample_id]

    # This will create a tuple with the item_type and with the corresponding associated item_ids'
    items_by_type = [
        (item_type,
         filtered_df[filtered_df.item_type == item_type].id.to_list())
        for item_type in filtered_df.item_type.unique()
    ]

    products_order = [{
        "item_type": k,
        "item_ids": v,
        "product_bundle": products_bundles[k]
    } for k, v in items_by_type]

    # clip to AOI
    aoi_geojson = json.loads(dumps(row.geometry))
    tools = [
        {
            'clip': {
                'aoi': aoi_geojson
            }
        },
    ]

    order_type = 'full'
    if partial:
        order_type = 'partial'

    order_request = {
        'name': f'{sufix}_{str(sample_id)}',
        'order_type': order_type,
        'products': products_order,
        'tools': tools,
        'delivery': {
            'single_archive': True,
            'archive_filename': '{{name}}.zip',
            'archive_type': 'zip'
        },
        'notifications': {
            'email': False
        },
    }
    return order_request
Пример #4
0
def getGeoJSON(covid_date_inc):
    print(f"covid_date_inc: {covid_date_inc}")
    features = createJSON(getDate(covid_date_inc))

    # json_docs = [json.dumps(feature) for feature in features]
    # resp = jsonify(data=json_docs)
    # return resp

    feature_collection = FeatureCollection(features)
    json_str = dumps(feature_collection, separators=(',', ':'))

    # The below returns a GeoJSON string that an online GeoJSON validator validated
    # When axios is used, instead of d3.json(), in the client the console displays
    # an array of features
    return json_str
Пример #5
0
def geojson_from_mask(mask,
                      thresh,
                      transform,
                      mode,
                      x_offset=0,
                      y_offset=0,
                      open_kernel=3,
                      close_kernel=3,
                      pixel_tolerence=0):
    features = []
    polys = geometries_from_mask(mask, thresh, transform, mode, x_offset,
                                 y_offset, open_kernel, close_kernel,
                                 pixel_tolerence)
    features = [Feature(p) for p in polys]
    feature_collection = FeatureCollection(features)
    return dumps(feature_collection, indent=2)
Пример #6
0
def buffer(input_args):

    #Casting to float
    lat = float(input_args["lat"])
    lon = float(input_args["lon"])
    range = float(input_args["range"])

    p = Point(lon, lat)

    buffer = p.buffer(1.0)
    feature = Feature(buffer, properties={'buffer_extent': '1'})
    json_response = dumps(feature, indent=2)
    resp = make_response(json_response, 200)

    resp.mimetype = "application/vnd.geo+json"  # So that browser knows what is the content type

    return resp
Пример #7
0
def geometry_filter(geometry):
    return {
        'type': 'GeometryFilter',
        'field_name': 'geometry',
        'config': json.loads(dumps(geometry))
    }
Пример #8
0
def handler(context, event):

    b = event.body
    if not isinstance(b, dict):
        body = json.loads(b.decode('utf-8-sig'))
    else:
        body = b
    
    context.logger.info("Event received !")

    try:

        # if we're not ready to handle this request yet, deny it
        if not FunctionState.done_loading:
            context.logger.warn_with('Function not ready, denying request !')
            raise NuclioResponseError(
                'The service is loading and is temporarily unavailable.',requests.codes.unavailable)

        # MongoDB infos
        db = FunctionConfig.target_db
        collection = FunctionConfig.natura_collection
        client = FunctionState.mongodb_client

        # MongoDB SOC collection
        natura = client[db][collection]

        # parse request's body
        feature = Helpers.parse_body(context,body)

        search_distance = FunctionConfig.default_search_distance
        if 'search' in event.fields:
            search_distance = int(event.fields['search'])

        # Get legal CRS
        props = feature['properties']
        if not ('crs' in props or 'legal_crs' in props):
            context.logger.warn_with('CRS of the feature not found in properties, please specify one !')
            raise NuclioResponseError(
                'CRS of the feature not found in properties, please specify one !',requests.codes.bad)
        crs_attr = 'legal_crs' if 'legal_crs' in props else 'crs'
        if 'type' in props[crs_attr] and props[crs_attr]['type'] == 'EPSG':
            code = props[crs_attr]['properties']['code']
            epsg_crs = "{0}:{1}".format(props[crs_attr]['type'],code)
        context.logger.info("Legal CRS: '{0}'".format(epsg_crs))

        # Reprojection function to legal CRS
        source = Proj(init=FunctionConfig.source_crs)
        target = Proj(init=epsg_crs)
        reproject = lambda x,y: pyproj.transform(source,target,x,y)

        # Get a buffer of the target feature
        buf = math.degrees((search_distance+100)/(6371 * 1000)) # in degree as we expected a EPSG:4326 CRS (WGS84)
        search = shape(feature['geometry']).buffer(buf)

        # Get natura2000 features intersecting the target feature
        natura_features = list(natura.find({
            "geometry": { 
                "$geoIntersects": { 
                    "$geometry": geojson.loads(shapely_geojson.dumps(search))
                } 
            } 
        }))

        # Reprojection of natura2000 geometries in legal CRS
        natura_geoms = [
            transform(reproject,shape(n['geometry']))
            for n in natura_features
        ]

        # Reprojection of target geometry in legal CRS
        parcel = transform(reproject,shape(feature['geometry']))

        for i, f in enumerate(natura_features):
            natura_geoms[i].value = f

        result = []
        for n in natura_geoms:
            d = parcel.distance(n)
            if d <= search_distance:
                r = {
                    "_id": n.value['_id'],
                    "intersects": False,
                    "minDistance": d,
                    "properties": n.value['properties']['natura'],
                    "wktType": n.type.upper()
                }
                r.update(n.value['properties'])
                r.pop('crs')
                r.pop('natura')
                r.pop('version')
                if parcel.intersects(n):
                    i = parcel.intersection(n)
                    r['intersects'] = True
                    r['intersection'] = i.area if i.area else i.length
                result.append(r)

        context.logger.info("'{0}' natura2000 features processed".format(len(natura_features)))

    except NuclioResponseError as error:
        return error.as_response(context)

    except Exception as error:
        context.logger.warn_with('Unexpected error occurred, responding with internal server error',
            exc=str(error))
        message = 'Unexpected error occurred: {0}\n{1}'.format(error, traceback.format_exc())
        return NuclioResponseError(message).as_response(context)

    return context.Response(body=json.dumps({ 'natura2000': result }),
                            headers={},
                            content_type='application/json',
                            status_code=requests.codes.ok)
Пример #9
0
from sys import argv
import json

# pip install fiona shapely shapely-geojson
import fiona
from shapely.geometry import shape
from shapely_geojson import dumps

if len(argv) < 2:
    print('usage: gen_map.py "New Mexico"')
    quit()
target_state = argv[1]
states = []
for pol in fiona.open('tl_2019_us_state/tl_2019_us_state.shp'):
    name = pol['properties']['NAME']
    if name == target_state:
        states.append(shape(pol['geometry']))

features = {"type": "FeatureCollection", "features": []}
for pol in fiona.open('tl_2019_us_aiannh/tl_2019_us_aiannh.shp'):
    geo = shape(pol['geometry'])
    for state in states:
        if geo.intersects(state):
            features["features"].append({
                "geometry": json.loads(dumps(geo)),
                "properties": pol['properties']
            })
            break
print(json.dumps(features, separators=(',', ':')))
Пример #10
0
#!/usr/bin/env python3

import shapefile
import shapely_geojson
from shapely.geometry import shape

shapefile = shapefile.Reader('tiling_grid/shape/sentinel2_tiles_world.shp')
records = shapefile.shapeRecords()
for feature in records:
    #print(feature.shape.__geo_interface__)
    print(feature.record[0])

    geom = shape(feature.shape).buffer(-0.3)
    geom2 = shapely_geojson.Feature(geom, properties={'name': feature.record[0]})
    #print(shapely_geojson.dumps(geom2))
    with open('tiling_grid/geojson/{0}.geojson'.format(feature.record[0]), 'w') as outfile:
        outfile.write(shapely_geojson.dumps(shapely_geojson.FeatureCollection([geom2])))
def test_dumps():
    point = Point(1, 1)
    geojson1 = '{\n "type": "Point",\n "coordinates": [\n  1.0,\n  1.0\n ]\n}'
    geojson2 = '{\n "coordinates": [\n  1.0,\n  1.0\n ],\n "type": "Point"\n}'
    dumped = dumps(point, indent=1)
    assert dumped == geojson1 or dumped == geojson2
Пример #12
0
def handler(context, event):

    b = event.body
    if not isinstance(b, dict):
        body = json.loads(b.decode('utf-8-sig'))
    else:
        body = b

    context.logger.info("Event received !")

    try:

        # if we're not ready to handle this request yet, deny it
        if not FunctionState.done_loading:
            context.logger.warn_with('Function not ready, denying request !')
            raise NuclioResponseError(
                'The service is loading and is temporarily unavailable.',
                requests.codes.unavailable)

        # MongoDB infos
        db = FunctionConfig.target_db
        collection = FunctionConfig.soc_collection
        client = FunctionState.mongodb_client

        # MongoDB SOC collection
        soc = client[db][collection]

        # parse request's body
        feature = Helpers.parse_body(context, body)

        # get original CRS of the feature
        props = feature['properties']
        if not ('crs' in props or 'legal_crs' in props):
            context.logger.warn_with(
                'CRS of the feature not found in properties, please specify one !'
            )
            raise NuclioResponseError(
                'CRS of the feature not found in properties, please specify one !',
                requests.codes.bad)
        crs_attr = 'legal_crs' if 'legal_crs' in props else 'crs'
        if 'type' in props[crs_attr] and props[crs_attr]['type'] == 'EPSG':
            code = props[crs_attr]['properties']['code']
            epsg_crs = "{0}:{1}".format(props[crs_attr]['type'], code)

        # set the according reprojection lambda function
        source = Proj(init=FunctionConfig.source_crs)
        context.logger.info("Source CRS: {0}".format(
            FunctionConfig.source_crs))
        target = Proj(init=epsg_crs)
        context.logger.info("Source CRS: {0}".format(epsg_crs))
        reproject = lambda x, y: pyproj.transform(source, target, x, y)

        ## get colocated SOC data within a buffer of the requested feature
        res = FunctionConfig.soc_resolution
        buf = math.degrees(
            res /
            (6371 * 1000))  # in degree as we expected a EPSG:4326 CRS (WGS84)
        search = shape(feature['geometry']).buffer(buf)

        # get SOC raster data
        geom = geojson.loads(shapely_geojson.dumps(search))
        soc_data = list(
            soc.find({"geometry": {
                "$geoWithin": {
                    "$geometry": geom
                }
            }}, {"_id": 0}))

        # get reprojected pixels
        soc_pixels = [
            transform(reproject, shape(p['geometry'])).buffer(res / 2,
                                                              cap_style=3)
            for p in soc_data
        ]

        # inject SOC value (elevation/z)
        soc_attr = 'soc'
        for i, v in enumerate(soc_data):
            soc_pixels[i].value = v['properties'][soc_attr]

        # reproject the requested feature to original CRS add add a buffer to smooth the computed result
        parcel = transform(reproject,
                           shape(feature['geometry'])).buffer(res / 4)
        s1, s2 = 0, 0
        # process a weighted value according to the parcel's footprint on each intersected pixel
        for px in soc_pixels:
            area = parcel.intersection(px).area
            s1 += area * px.value
            s2 += area
        # update the parcel with the weighted value if at least one pixel intersects it
        if s2 != 0:
            soc_average = round(s1 / s2, 6)  # arbitrary

        context.logger.info("'{0}' soc pixels processed".format(len(soc_data)))

    except NuclioResponseError as error:
        return error.as_response(context)

    except Exception as error:
        context.logger.warn_with(
            'Unexpected error occurred, responding with internal server error',
            exc=str(error))
        message = 'Unexpected error occurred: {0}\n{1}'.format(
            error, traceback.format_exc())
        return NuclioResponseError(message).as_response(context)

    return context.Response(body=json.dumps({'soc': soc_average}),
                            headers={},
                            content_type='application/json',
                            status_code=requests.codes.ok)
Пример #13
0
def getGeoJSON(covid_date_inc):
    features = createJSON(getDate(covid_date_inc))

    feature_collection = FeatureCollection(features)
    json_str = dumps(feature_collection, separators=(',', ':'))
    return json_str
Пример #14
0
def serialise(winery):

    feature = create_feature(winery)

    return dumps(feature, indent=2)
Пример #15
0
    parser.add_argument('--outputpath',
                        required=True,
                        dest="outputpath",
                        help='The path to the output json file')

    args = parser.parse_args()

    data = pd.read_csv(args.inputpath, sep='\t', header=0, encoding='utf8')

    grouped = data[['Winery', 'lat',
                    'lon']].groupby(['Winery', 'lat',
                                     'lon']).size().reset_index(name='count')

    features = list()

    for i in grouped.itertuples():

        nextwinery = Winery(i[1], float(i[2]), float(i[3]), i[4])

        nextfeature = create_feature(nextwinery)

        features.append(nextfeature)

    feature_collection = FeatureCollection(features)

    json = dumps(feature_collection, indent=2, ensure_ascii=False)

    with codecs.open(args.outputpath, 'w', encoding="utf-8") as outfile:

        outfile.write(json)
Пример #16
0
 def get_geojson_feature(self, properties={}):
     feature = Feature(self.shape, properties=properties)
     return json.loads(dumps(feature))
Пример #17
0
def main(argv):
    pc2points_nogeo = {}
    with closing(io.open('bagadres-full.csv')) as io_file:
        rows_read = 0
        old_nr = 0
        last_point = None
        for row in csv.reader(io_file, delimiter=';'):
            rows_read += 1
            if rows_read == 1:
                continue
            # if rows_read % 100 == 0:
            #     print(rows_read)
            if rows_read % 10000 == 0:
                break
            if row[1] == old_nr:
                continue
            old_nr = row[1]
            p = shapely.geometry.Point(float(row[-2]), float(row[-1]))
            if p == last_point:
                continue
            last_point = p
            pc = row[4]
            try:
                pc2points_nogeo[pc].append(p)
            except Exception as e:
                pc2points_nogeo[pc] = [p]
                #    p.coords[:] + p.coords[:] + p.coords[:])

    pc2points = {p: shapely.geometry.MultiPoint(q) for p, q in pc2points_nogeo.items()}
    output = []
    v_input = []
    for p, poly in pc2points.items():
         for p in poly.geoms:
            v_input += p.coords

    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
    area = world[world.name == 'Netherlands']

    area = area.to_crs(epsg=3395)    # convert to World Mercator CRS
    area_shape = area.iloc[0].geometry   # get the Polygon
    # Recoger todas las lineas y armarlas en poligonos.
    vor = Voronoi(v_input)
    lines = [shapely.geometry.LineString(vor.vertices[line]) for line in vor.ridge_vertices]
    last_pc = ""
    last_polys = []
    polys_found = 0
    polys = 0
    result_polys = []
    pc_polys = {}
    for poly in shapely.ops.polygonize(lines):
        polys += 1
        pc = ""
        old_polys_found = polys_found
        for p2, poly2 in pc2points.items():
            for p in poly2.geoms:
                if poly.contains(p):
                    pc = p2
                    polys_found += 1
                    try:
                        pc_polys[pc].append(poly)
                    except LookupError as e:
                        pc_polys[pc] = [poly]
                    break
        if old_polys_found == polys_found:
            result_polys.append(Feature(poly, {'postcode': None}))

    for pc, polys in pc_polys.items():
        result_polys.append(
            Feature(shapely.ops.unary_union(polys), {'postcode': pc}))

    print(dumps(FeatureCollection(result_polys), indent=2))
    return 0
Пример #18
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Necessary pip install: shapely and shapely_geojson
#sudo pip install shapely shapely_geojson
#sudo pip install git+https://github.com/alekzvik/shapely-geojson

from shapely.geometry.point import Point
from shapely_geojson import dumps, Feature
p = Point(-8.2918, 41.4425)  # (lon,lat) 41.4425° N, 8.2918° W

buffer = p.buffer(1.0)
feature = Feature(buffer, properties={'buffer_extent': '1'})
f1 = open("./data/guimaraes_buffer.geojson", "w")
f1.write(dumps(feature, indent=2))
Пример #19
0
        if valid != 'Valid Geometry':
            print('ERREUR:', nom, valid)
            exit()
        if Polygon(poly).area > 0.01 and nom not in ['CSG KOUROU 1']:
            print('ERREUR:', nom, 'emprise trop importante')
            exit()
        return (Feature(Polygon(poly), {'nom': nom}))


pts = []
nom = None
features = []

for ligne in txt:
    ligne = ligne[:-1]
    match = re.match(check, ligne)
    if match:
        pts.append(match.group(1, 2, 3, 4, 6, 7, 8, 9, 11, 13))
    else:
        if nom:
            features.append(pts2feature(pts, nom))
            nom = None
        if ligne != '':
            nom = ligne
            pts = []

if nom:
    features.append(pts2feature(pts, nom))

print(dumps(FeatureCollection(features)))