|
45 | 45 | " if i >= start_index:\n", |
46 | 46 | " yield obj\n", |
47 | 47 | "\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", |
50 | 54 | "\n", |
51 | 55 | " for feature in features:\n", |
52 | 56 | " feature_id = feature.get(\"id\")\n", |
|
69 | 73 | " gdf = gdf.to_crs(epsg=4326)\n", |
70 | 74 | " return gdf\n", |
71 | 75 | "\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", |
72 | 102 | "collection_id = \"ships\"" |
73 | 103 | ] |
74 | 104 | }, |
|
87 | 117 | "metadata": {}, |
88 | 118 | "outputs": [], |
89 | 119 | "source": [ |
90 | | - "# resp = requests.delete(f\"{HOST}/collections/{\"ships\"}\")\n", |
| 120 | + "resp = requests.delete(f\"{HOST}/collections/{\"ships\"}\")\n", |
91 | 121 | "\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", |
94 | 124 | "\n", |
95 | | - "# print(resp.status_code)\n" |
| 125 | + "print(resp.status_code)\n" |
96 | 126 | ] |
97 | 127 | }, |
98 | 128 | { |
|
191 | 221 | "outputs": [], |
192 | 222 | "source": [ |
193 | 223 | "\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", |
261 | 291 | " \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", |
266 | 296 | " \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", |
272 | 302 | " \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", |
282 | 312 | " \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", |
293 | 323 | " \n" |
294 | 324 | ] |
295 | 325 | }, |
|
365 | 395 | " gdf,\n", |
366 | 396 | " name=\"All ships\", \n", |
367 | 397 | " 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", |
369 | 399 | ").add_to(m)\n", |
370 | | - "fl.LayerControl(collapsed=False).add_to(m)\n", |
| 400 | + "\n", |
| 401 | + "\n", |
371 | 402 | "m" |
372 | 403 | ] |
373 | 404 | }, |
|
388 | 419 | "source": [ |
389 | 420 | "PORT_BBOX = \"651135,6058230,651422,6058548\" # Rodby harbour envelope (meters, EPSG:25832)\n", |
390 | 421 | "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", |
392 | 423 | "\n", |
393 | 424 | "print(\"GET MOVING FEATURES BY BBOX:\")\n", |
394 | 425 | "print(\"RESPONSE CODE:\", resp.status_code)\n", |
|
408 | 439 | "from shapely.geometry import box\n", |
409 | 440 | "import geopandas as gpd\n", |
410 | 441 | "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", |
413 | 449 | "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", |
418 | 458 | ").add_to(m)\n", |
419 | 459 | "\n", |
420 | | - "fl.LayerControl(collapsed=False).add_to(m)\n", |
| 460 | + "\n", |
| 461 | + "\n", |
421 | 462 | "minx, miny, maxx, maxy = map(float, PORT_BBOX.split(\",\"))\n", |
422 | 463 | "\n", |
423 | 464 | "envelope = box(minx, miny, maxx, maxy)\n", |
|
427 | 468 | " geometry=[envelope],\n", |
428 | 469 | " crs=\"EPSG:25832\"\n", |
429 | 470 | ").to_crs(4326)\n", |
430 | | - "fl.GeoJson(\n", |
| 471 | + "layer = fl.GeoJson(\n", |
431 | 472 | " gdf_env,\n", |
432 | 473 | " name=\"Port envelope\",\n", |
| 474 | + " tooltip=\"Rodby\",\n", |
433 | 475 | " style_function=lambda x: {\n", |
434 | 476 | " \"color\": \"black\",\n", |
435 | 477 | " \"weight\": 2,\n", |
436 | 478 | " \"fillOpacity\": 0.1\n", |
437 | 479 | " }\n", |
438 | 480 | ").add_to(m)\n", |
439 | | - "fl.LayerControl(collapsed=False).add_to(m)\n", |
| 481 | + "\n", |
440 | 482 | "\n", |
441 | 483 | "m\n" |
442 | 484 | ] |
|
539 | 581 | "\n", |
540 | 582 | "resp = requests.get(\n", |
541 | 583 | " 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", |
543 | 585 | ")\n", |
544 | 586 | "print(resp.url)\n", |
545 | 587 | "print(\"GET Ships subtrajectory\")\n", |
|
559 | 601 | "features = resp_json.get(\"features\", [])\n", |
560 | 602 | "\n", |
561 | 603 | "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", |
562 | 607 | "fl.GeoJson(\n", |
563 | 608 | " gdf,\n", |
564 | 609 | " 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", |
566 | 611 | ").add_to(m)\n", |
567 | 612 | "\n", |
568 | 613 | "fl.LayerControl(collapsed=False).add_to(m)\n", |
569 | | - "m\n" |
| 614 | + "m" |
570 | 615 | ] |
571 | 616 | } |
572 | 617 | ], |
|
0 commit comments