Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion configs/seed_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,34 @@
"style": { "paint": { "line-color": "hsl(248,7%,66%)" } }
},
{
"id": "inventaire",
"id": "seed",
"type": "datapackage",
"layerType": "circle",
"path": "../catalog/seed/datapackage.json",
"resource": "survey",
"columns": {
"geom": "geom",
"id": "OGC_FID",
"orga": "votre_orga",
"responsable": "nom_du_res",
"date_plantation": "date_de_pl",
"type_plant": "type_de_pl",
"prelevement_mangrove": "trace_de_p",
"test": "round(random() * 4 ) + 1"
},
"popup": {
"trigger": "click"
}
},
{
"id": "seed_point",
"type": "datapackage",
"path": "../catalog/seed/datapackage.json",
"resource": "survey",
"columns": {
"geom": "centroid(geom)"
},
"maxzoom": 13
}
],
"controls": [
Expand Down
31 changes: 19 additions & 12 deletions coordo-py/coordo/map/datapackage.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,15 @@ class DataPackageLayer(BaseLayerModel):
filter: str | None = None
groupby: list[str] | None = None
columns: dict[str, str] | None = None
layerType: str | None = None
popup: Popup | None = None

def to_maplibre(self, base_path):
package = DataPackage.from_path(base_path / self.path)
resource = package.get_resource(name=self.resource)
data = self.get_data(base_path=base_path)

# We check the type of the first non-null geometry, it doesn't support yet mixed geometries
geom_type = (
next(f["geometry"] for f in data["features"] if f["geometry"])["type"]
if data["features"]
else "Point"
)
if "Polygon" in geom_type:
layer_type = "fill"
elif "LineString" in geom_type:
layer_type = "line"
else:
layer_type = "circle"
layer_type = self.layerType or self.infer_layer_type(data["features"])

source = GeoJSONSource(type="geojson", data=data)
metadata = {
Expand Down Expand Up @@ -97,3 +87,20 @@ def get_data(self, *, base_path, filter=None) -> FeatureCollection:
)
assert isinstance(df, GeoDataFrame), "No geometry column found."
return df.to_geo_dict(show_bbox=True) # type: ignore

def infer_layer_type(self, features):
# We check the type of the first non-null geometry, it doesn't support yet mixed geometries
geom_type = (
next(f["geometry"] for f in features if f["geometry"])["type"]
if features
else "Point"
)
if "Polygon" in geom_type:
layer_type = "fill"
elif "LineString" in geom_type:
layer_type = "line"
else:
layer_type = "circle"

return layer_type

Loading