Skip to content

Commit e558191

Browse files
committed
demo
1 parent cf447bd commit e558191

1 file changed

Lines changed: 161 additions & 116 deletions

File tree

demo.ipynb

Lines changed: 161 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@
4545
" if i >= start_index:\n",
4646
" yield obj\n",
4747
"\n",
48-
"def json_to_gdf(features,geoms=[],ids=[]):\n",
49-
" \n",
48+
"def json_to_gdf(features,geoms=None,ids=None):\n",
49+
" if geoms is None:\n",
50+
" geoms = []\n",
51+
" if ids is None:\n",
52+
" ids = []\n",
53+
"\n",
5054
"\n",
5155
" for feature in features:\n",
5256
" feature_id = feature.get(\"id\")\n",
@@ -69,6 +73,32 @@
6973
" gdf = gdf.to_crs(epsg=4326)\n",
7074
" return gdf\n",
7175
"\n",
76+
"\n",
77+
"\n",
78+
"\n",
79+
"from shapely.geometry import shape\n",
80+
"\n",
81+
"def json_to_gdf2(features):\n",
82+
" geoms = []\n",
83+
" ids = []\n",
84+
"\n",
85+
" for feature in features:\n",
86+
" geom = feature.get(\"geometry\")\n",
87+
" if not geom:\n",
88+
" continue\n",
89+
"\n",
90+
" try:\n",
91+
" shapely_geom = shape(geom)\n",
92+
" except Exception:\n",
93+
" continue\n",
94+
"\n",
95+
" geoms.append(shapely_geom)\n",
96+
" ids.append(feature.get(\"id\"))\n",
97+
"\n",
98+
" gdf = gpd.GeoDataFrame({\"id\": ids}, geometry=geoms, crs=\"EPSG:4326\")\n",
99+
" # gdf = gpd.GeoDataFrame({\"id\": ids}, geometry=geoms, crs=\"EPSG:25832\")\n",
100+
" # gdf = gdf.to_crs(epsg=4326)\n",
101+
" return gdf\n",
72102
"collection_id = \"ships\""
73103
]
74104
},
@@ -87,12 +117,12 @@
87117
"metadata": {},
88118
"outputs": [],
89119
"source": [
90-
"# resp = requests.delete(f\"{HOST}/collections/{\"ships\"}\")\n",
120+
"resp = requests.delete(f\"{HOST}/collections/{\"ships\"}\")\n",
91121
"\n",
92-
"# print(resp.status_code)\n",
93-
"# resp = requests.delete(f\"{HOST}/collections/{\"planes\"}\")\n",
122+
"print(resp.status_code)\n",
123+
"resp = requests.delete(f\"{HOST}/collections/{\"planes\"}\")\n",
94124
"\n",
95-
"# print(resp.status_code)\n"
125+
"print(resp.status_code)\n"
96126
]
97127
},
98128
{
@@ -191,105 +221,105 @@
191221
"outputs": [],
192222
"source": [
193223
"\n",
194-
"# batch_size = 20 # max96\n",
195-
"# created_count = 0\n",
196-
"# batch = []\n",
197-
"\n",
198-
"# for obj in iter_features_from_index(data , start_index=0):\n",
199-
"# batch.append(obj)\n",
200-
"# if len(batch) >= batch_size:\n",
201-
"# features = []\n",
202-
"# for o in batch:\n",
203-
"# features.append({\n",
204-
"# \"type\": \"Feature\",\n",
205-
"# \"id\": str(o[\"mmsi\"]),\n",
206-
"# \"properties\": o[\"properties\"],\n",
207-
"# \"crs\": {\n",
208-
"# \"type\": \"name\",\n",
209-
"# \"properties\": {\n",
210-
"# \"name\": \"urn:ogc:def:crs:EPSG::25832\"\n",
211-
"# }\n",
212-
"# },\n",
213-
"# \"trs\": {\n",
214-
"# \"type\": \"Link\",\n",
215-
"# \"properties\": {\n",
216-
"# \"type\": \"ogcdef\",\n",
217-
"# \"href\": \"http://www.opengis.net/def/uom/ISO-8601/0/Gregorian\"\n",
218-
"# }\n",
219-
"# },\n",
220-
"# \"temporalGeometry\": json.loads(o[\"trajectory\"]),\n",
221-
"# \"temporalProperties\": [{\n",
222-
"# \"datetimes\": [\n",
223-
"# \"2011-07-14T22:01:01.450Z\",\n",
224-
"# \"2011-07-14T23:01:01.450Z\",\n",
225-
"# \"2011-07-15T00:01:01.450Z\"\n",
226-
"# ],\n",
227-
"# \"length\": {\n",
228-
"# \"type\": \"Measure\",\n",
229-
"# \"form\": \"http://qudt.org/vocab/quantitykind/Length\",\n",
230-
"# \"values\": [1, 2.4, 1],\n",
231-
"# \"interpolation\": \"Linear\",\n",
232-
"# \"description\": \"description1\"\n",
233-
"# },\n",
234-
"# \"discharge\": {\n",
235-
"# \"type\": \"Measure\",\n",
236-
"# \"form\": \"MQS\",\n",
237-
"# \"values\": [3, 4, 5],\n",
238-
"# \"interpolation\": \"Step\"\n",
239-
"# }\n",
240-
"# },\n",
241-
"# {\n",
242-
"# \"datetimes\": [\n",
243-
"# \"2011-07-15T23:01:01.450Z\",\n",
244-
"# \"2011-07-16T00:01:01.450Z\"\n",
245-
"# ],\n",
246-
"# \"camera\": {\n",
247-
"# \"type\": \"Image\",\n",
248-
"# \"values\": [\n",
249-
"# \"http://.../example/image1\",\n",
250-
"# \"VBORw0KGgoAAAANSUhEU......\"\n",
251-
"# ],\n",
252-
"# \"interpolation\": \"Discrete\"\n",
253-
"# },\n",
254-
"# \"labels\": {\n",
255-
"# \"type\": \"Text\",\n",
256-
"# \"values\": [\"car\", \"human\"],\n",
257-
"# \"interpolation\": \"Discrete\"\n",
258-
"# }\n",
259-
"# }]\n",
260-
"# })\n",
224+
"batch_size = 20 # max96\n",
225+
"created_count = 0\n",
226+
"batch = []\n",
227+
"\n",
228+
"for obj in iter_features_from_index(data , start_index=0):\n",
229+
" batch.append(obj)\n",
230+
" if len(batch) >= batch_size:\n",
231+
" features = []\n",
232+
" for o in batch:\n",
233+
" features.append({\n",
234+
" \"type\": \"Feature\",\n",
235+
" \"id\": str(o[\"mmsi\"]),\n",
236+
" \"properties\": o[\"properties\"],\n",
237+
" \"crs\": {\n",
238+
" \"type\": \"name\",\n",
239+
" \"properties\": {\n",
240+
" \"name\": \"urn:ogc:def:crs:EPSG::25832\"\n",
241+
" }\n",
242+
" },\n",
243+
" \"trs\": {\n",
244+
" \"type\": \"Link\",\n",
245+
" \"properties\": {\n",
246+
" \"type\": \"ogcdef\",\n",
247+
" \"href\": \"http://www.opengis.net/def/uom/ISO-8601/0/Gregorian\"\n",
248+
" }\n",
249+
" },\n",
250+
" \"temporalGeometry\": json.loads(o[\"trajectory\"]),\n",
251+
" \"temporalProperties\": [{\n",
252+
" \"datetimes\": [\n",
253+
" \"2011-07-14T22:01:01.450Z\",\n",
254+
" \"2011-07-14T23:01:01.450Z\",\n",
255+
" \"2011-07-15T00:01:01.450Z\"\n",
256+
" ],\n",
257+
" \"length\": {\n",
258+
" \"type\": \"Measure\",\n",
259+
" \"form\": \"http://qudt.org/vocab/quantitykind/Length\",\n",
260+
" \"values\": [1, 2.4, 1],\n",
261+
" \"interpolation\": \"Linear\",\n",
262+
" \"description\": \"description1\"\n",
263+
" },\n",
264+
" \"discharge\": {\n",
265+
" \"type\": \"Measure\",\n",
266+
" \"form\": \"MQS\",\n",
267+
" \"values\": [3, 4, 5],\n",
268+
" \"interpolation\": \"Step\"\n",
269+
" }\n",
270+
" },\n",
271+
" {\n",
272+
" \"datetimes\": [\n",
273+
" \"2011-07-15T23:01:01.450Z\",\n",
274+
" \"2011-07-16T00:01:01.450Z\"\n",
275+
" ],\n",
276+
" \"camera\": {\n",
277+
" \"type\": \"Image\",\n",
278+
" \"values\": [\n",
279+
" \"http://.../example/image1\",\n",
280+
" \"VBORw0KGgoAAAANSUhEU......\"\n",
281+
" ],\n",
282+
" \"interpolation\": \"Discrete\"\n",
283+
" },\n",
284+
" \"labels\": {\n",
285+
" \"type\": \"Text\",\n",
286+
" \"values\": [\"car\", \"human\"],\n",
287+
" \"interpolation\": \"Discrete\"\n",
288+
" }\n",
289+
" }]\n",
290+
" })\n",
261291
" \n",
262-
"# feature_collection = {\n",
263-
"# \"type\": \"FeatureCollection\",\n",
264-
"# \"features\": features\n",
265-
"# }\n",
292+
" feature_collection = {\n",
293+
" \"type\": \"FeatureCollection\",\n",
294+
" \"features\": features\n",
295+
" }\n",
266296
" \n",
267-
"# resp = requests.post(\n",
268-
"# f\"{HOST}/collections/{collection_id}/items\",\n",
269-
"# json=feature_collection,\n",
270-
"# headers={\"Content-Type\": \"application/json\"}\n",
271-
"# )\n",
297+
" resp = requests.post(\n",
298+
" f\"{HOST}/collections/{collection_id}/items\",\n",
299+
" json=feature_collection,\n",
300+
" headers={\"Content-Type\": \"application/json\"}\n",
301+
" )\n",
272302
" \n",
273-
"# if resp.status_code in (201, 409):\n",
274-
"# created_count += len(batch)\n",
275-
"# print(f\"Batch: Created {len(batch)} features\")\n",
276-
"# batch = [] \n",
277-
"\n",
278-
"# feature_collection = {\n",
279-
"# \"type\": \"FeatureCollection\",\n",
280-
"# \"features\": features\n",
281-
"# }\n",
303+
" if resp.status_code in (201, 409):\n",
304+
" created_count += len(batch)\n",
305+
" print(f\"Batch: Created {len(batch)} features\")\n",
306+
" batch = [] \n",
307+
"\n",
308+
"feature_collection = {\n",
309+
" \"type\": \"FeatureCollection\",\n",
310+
" \"features\": features\n",
311+
" }\n",
282312
" \n",
283-
"# resp = requests.post(\n",
284-
"# f\"{HOST}/collections/{collection_id}/items\",\n",
285-
"# json=feature_collection,\n",
286-
"# headers={\"Content-Type\": \"application/json\"}\n",
287-
"# )\n",
288-
"# print(\"POST MOVING FEATURES:\")\n",
289-
"# print(\"RESPONSE CODE:\", resp.status_code)\n",
290-
"# resp_json = resp.json()\n",
291-
"# # print(resp.headers)\n",
292-
"# # print(\"Response object\",json.dumps(resp_json, indent=2))\n",
313+
"resp = requests.post(\n",
314+
" f\"{HOST}/collections/{collection_id}/items\",\n",
315+
" json=feature_collection,\n",
316+
" headers={\"Content-Type\": \"application/json\"}\n",
317+
")\n",
318+
"print(\"POST MOVING FEATURES:\")\n",
319+
"print(\"RESPONSE CODE:\", resp.status_code)\n",
320+
"resp_json = resp.json()\n",
321+
"# print(resp.headers)\n",
322+
"# print(\"Response object\",json.dumps(resp_json, indent=2))\n",
293323
" \n"
294324
]
295325
},
@@ -365,9 +395,10 @@
365395
" gdf,\n",
366396
" name=\"All ships\", \n",
367397
" tooltip=fl.GeoJsonTooltip(fields=[\"id\"], aliases=[\"Ship ID:\"]),\n",
368-
" style_function=lambda x: {\"color\": \"brown\", \"weight\": 1}\n",
398+
" style_function=lambda x: {\"color\": \"brown\", \"weight\": 1, \"fillOpacity\": 0.6}\n",
369399
").add_to(m)\n",
370-
"fl.LayerControl(collapsed=False).add_to(m)\n",
400+
"\n",
401+
"\n",
371402
"m"
372403
]
373404
},
@@ -388,7 +419,7 @@
388419
"source": [
389420
"PORT_BBOX = \"651135,6058230,651422,6058548\" # Rodby harbour envelope (meters, EPSG:25832)\n",
390421
"resp = requests.get(\n",
391-
" f\"{HOST}/collections/{collection_id}/items?bbox={PORT_BBOX}\")\n",
422+
" f\"{HOST}/collections/{collection_id}/items?bbox={PORT_BBOX}&limit=97\")\n",
392423
"\n",
393424
"print(\"GET MOVING FEATURES BY BBOX:\")\n",
394425
"print(\"RESPONSE CODE:\", resp.status_code)\n",
@@ -408,16 +439,26 @@
408439
"from shapely.geometry import box\n",
409440
"import geopandas as gpd\n",
410441
"data_bbox = resp.json()\n",
411-
"features_bbox = data_bbox.get(\"features\", [])\n",
412-
"gdf = json_to_gdf(features_bbox)\n",
442+
"features_bbox = resp_json.get(\"features\", [])\n",
443+
"geoms = []\n",
444+
"ids = []\n",
445+
"gdf = json_to_gdf(features_bbox,geoms, ids)\n",
446+
"fig = Figure(width=\"900px\", height=\"400px\")\n",
447+
"b = fl.Map(location=[54.6, 11.0], zoom_start=6, tiles=\"OpenStreetMap\",width=\"100%\", height=\"500px\")\n",
448+
"fig.add_child(b)\n",
413449
"fl.GeoJson(\n",
414-
"gdf,\n",
415-
"name=\"Bbox ships\",\n",
416-
"tooltip=fl.GeoJsonTooltip(fields=[\"id\"], aliases=[\"Ship ID:\"]),\n",
417-
"style_function=lambda x: {\"color\": \"blue\", \"weight\": 1}\n",
450+
" gdf,\n",
451+
" name=\"Bbox ships\",\n",
452+
" tooltip=fl.GeoJsonTooltip(fields=[\"id\"], aliases=[\"Ship ID:\"]),\n",
453+
" style_function=lambda x: {\n",
454+
" \"color\": \"blue\",\n",
455+
" \"weight\": 1,\n",
456+
" \"fillOpacity\": 1},\n",
457+
" show=True\n",
418458
").add_to(m)\n",
419459
"\n",
420-
"fl.LayerControl(collapsed=False).add_to(m)\n",
460+
"\n",
461+
"\n",
421462
"minx, miny, maxx, maxy = map(float, PORT_BBOX.split(\",\"))\n",
422463
"\n",
423464
"envelope = box(minx, miny, maxx, maxy)\n",
@@ -427,16 +468,17 @@
427468
" geometry=[envelope],\n",
428469
" crs=\"EPSG:25832\"\n",
429470
").to_crs(4326)\n",
430-
"fl.GeoJson(\n",
471+
"layer = fl.GeoJson(\n",
431472
" gdf_env,\n",
432473
" name=\"Port envelope\",\n",
474+
" tooltip=\"Rodby\",\n",
433475
" style_function=lambda x: {\n",
434476
" \"color\": \"black\",\n",
435477
" \"weight\": 2,\n",
436478
" \"fillOpacity\": 0.1\n",
437479
" }\n",
438480
").add_to(m)\n",
439-
"fl.LayerControl(collapsed=False).add_to(m)\n",
481+
"\n",
440482
"\n",
441483
"m\n"
442484
]
@@ -539,7 +581,7 @@
539581
"\n",
540582
"resp = requests.get(\n",
541583
" f\"{HOST}/collections/{collection_id}/items\",\n",
542-
" params={\"subTrajectory\": \"true\", \"datetime\": TIME_INTERVAL}\n",
584+
" params={\"subTrajectory\": \"true\", \"datetime\": TIME_INTERVAL,\"limit\":\"137\"}\n",
543585
")\n",
544586
"print(resp.url)\n",
545587
"print(\"GET Ships subtrajectory\")\n",
@@ -559,14 +601,17 @@
559601
"features = resp_json.get(\"features\", [])\n",
560602
"\n",
561603
"gdf = json_to_gdf(features)\n",
604+
"fig = Figure(width=\"900px\", height=\"400px\")\n",
605+
"b = fl.Map(location=[54.6, 11.0], zoom_start=6, tiles=\"cartodbpositron\",width=\"100%\", height=\"500px\")\n",
606+
"fig.add_child(b)\n",
562607
"fl.GeoJson(\n",
563608
" gdf,\n",
564609
" name=\"Subtrajectories ships 10:30-11:30\",\n",
565-
" style_function=lambda x: {\"color\": \"yellow\", \"weight\": 1}\n",
610+
" style_function=lambda x: {\"color\": \"yellow\", \"weight\": 2, \"opacity\": 1}\n",
566611
").add_to(m)\n",
567612
"\n",
568613
"fl.LayerControl(collapsed=False).add_to(m)\n",
569-
"m\n"
614+
"m"
570615
]
571616
}
572617
],

0 commit comments

Comments
 (0)