def test_feature_collection_insert(self):
        fc = FeatureCollection([self.feature1])

        fc.insert(0, self.feature2)

        assert len(fc["features"]) == 2
        assert fc["features"][1] == self.feature1
        assert fc["features"][0] == self.feature2
    def test_feature_collection_extend(self):
        fc1 = FeatureCollection([self.feature1])
        fc2 = FeatureCollection([self.feature2])

        fc3 = fc1 + fc2

        assert len(fc3["features"]) == 2
        assert len(fc1["features"]) == 1
        assert len(fc2["features"]) == 1
        assert fc3["features"][0] == self.feature1
        assert fc3["features"][1] == self.feature2
Beispiel #3
0
def read_many(request):
    features = []
    for layer in _get_layers_for_request(request):
        for f in _proto_read(layer, request).features:
            f.properties['__layer_id__'] = layer.id
            features.append(f)
    return FeatureCollection(features)
Beispiel #4
0
def hexagons_dataframe_to_geojson(df_hex, file_output=None):
    """
    Produce the GeoJSON for a dataframe that has a geometry column in geojson 
    format already, along with the columns hex_id and value
    
    Ex counts_by_hexagon(data)
    """
    list_features = []

    for i, row in df_hex.iterrows():
        feature = Feature(geometry=row["geometry"],
                          id=row["hex_id"],
                          properties={"value": row["value"]})
        list_features.append(feature)

    feat_collection = FeatureCollection(list_features)

    geojson_result = json.dumps(feat_collection)

    #optionally write to file
    if file_output is not None:
        with open(file_output, "w") as f:
            json.dump(feat_collection, f)

    return geojson_result
    def test_feature_collection_radd(self):
        fc = FeatureCollection([self.feature1, self.feature2])

        list1 = [] + fc

        assert len(list1) == 2
        assert isinstance(list1, list)
        assert list1[0] == self.feature1
        assert list1[1] == self.feature2
Beispiel #6
0
    def read_many(self):
        set_common_headers(self.request, "layers", NO_CACHE)

        features = []
        for layer in self._get_layers_for_request():
            for f in self._proto_read(layer).features:
                f.properties["__layer_id__"] = layer.id
                features.append(f)

        return FeatureCollection(features)
Beispiel #7
0
def _proto_read(layer, request):
    """ Read features for the layer based on the request. """
    if layer.public:
        return _get_protocol_for_layer(layer).read(request)
    if request.user is None:
        return FeatureCollection([])
    user = request.user
    proto = _get_protocol_for_layer(layer)
    cls = proto.mapped_class
    geom_attr = proto.geom_attr
    ra = DBSession.query(RestrictionArea.area.collect)
    ra = ra.join(RestrictionArea.roles)
    ra = ra.join(RestrictionArea.layers)
    ra = ra.filter(RestrictionArea.area.area > 0)
    ra = ra.filter(Role.id == user.role.id)
    ra = ra.filter(Layer.id == layer.id).scalar()
    ra = DBSpatialElement(ra)
    filter_ = and_(create_filter(request, cls, geom_attr),
                   ra.gcontains(getattr(cls, geom_attr)))
    return proto.read(request, filter=filter_)
def hexagons_dataframe_to_geojson(
    df_hex, id_field, geometry_field, value_field, file_output=None
):
    """Produce the GeoJSON representation containing all geometries in a dataframe
    based on a column in geojson format (geometry_field).

    Parameters
    ----------
    df_hex : DataFrame, required
        The dataframe where one row represents a specific geometric shape and a value.
    id_field : string, required
        The column name of a column which serves as a unique identifier.
    geometry_field: string, required
        The column name of the column containing the geometric shape in geojson format.
    value_field : string, required
        The column name of the column containing values that should be appended to the
        results as properties named "value".
    """

    list_features = []

    for _, row in df_hex.iterrows():
        feature = Feature(
            geometry=row[geometry_field],
            id=row[id_field],
            properties={"value": row[value_field]},
        )
        list_features.append(feature)

    feat_collection = FeatureCollection(list_features)

    geojson_result = json.dumps(feat_collection)

    # optionally write to file
    if file_output is not None:
        with open(file_output, "w") as f:
            json.dump(feat_collection, f)

    return geojson_result
    # create list for huell
    feature_list = list()

    print("merge_geojson_files started")
    print("input file directory {}".format(options.InDir))
    print("output file name     {}".format(options.OutFile))

    # loop over all files and merge
    for filename in filenamelist:

        with open(options.InDir + filename) as f:
            content = f.read()
            gj_data = geojson.loads(content)

            feature_list.append(gj_data)

    jsonres = FeatureCollection(feature_list)

    if jsonres.is_valid is not True:
        print(jsonres.errors())

    # print created geojson object to console
    # print(geojson.dumps(jsonres, indent=4, sort_keys=True))

    # print created geojson object to output file
    with open(options.OutFile, 'w') as f:
        f.write(geojson.dumps(jsonres, indent=4, sort_keys=True))

    print("exit merge_geojson_files")