Skip to content

Instantly share code, notes, and snippets.

@symbioquine
Created July 15, 2025 16:35
Show Gist options
  • Select an option

  • Save symbioquine/160d435a951523c52c86f9327f8ba2c4 to your computer and use it in GitHub Desktop.

Select an option

Save symbioquine/160d435a951523c52c86f9327f8ba2c4 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"kernelspec": {
"name": "python",
"display_name": "Python (Pyodide)",
"language": "python"
},
"language_info": {
"codemirror_mode": {
"name": "python",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8"
}
},
"nbformat_minor": 5,
"nbformat": 4,
"cells": [
{
"id": "e9079dc9-2acf-446d-a9d0-3ba82c1571aa",
"cell_type": "code",
"source": "from pyodide.http import pyfetch\nfrom js import location\n\nasync def get_all_entities(base_url):\n next_page = base_url\n while next_page is not None:\n resp = await pyfetch(next_page, method='GET')\n\n data = await resp.json()\n\n for entity in data.get('data', []):\n yield entity\n\n next_page = data.get('links', {}).get('next', {}).get('href', None)\n\nall_asset_types = [q['attributes']['drupal_internal__id'] async for q in get_all_entities(location.origin + '/api/asset_type/asset_type')]\n\nprint(all_asset_types)\n\nasync def query_all_locations():\n for asset_type in all_asset_types:\n async for q in get_all_entities(location.origin + f'/api/asset/{asset_type}?filter[is_location]=1&filter[status]=active'):\n yield q\n\nall_locations = [q async for q in query_all_locations()]\n\nprint(len(all_locations))",
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "['animal', 'equipment', 'group', 'land', 'material', 'money', 'plant', 'seed', 'structure', 'water']\n432\n"
}
],
"execution_count": 70
},
{
"id": "fcebf342-0dd8-47a8-9a58-60458145d8bf",
"cell_type": "code",
"source": "import piplite\nawait piplite.install(\"shapely\")",
"metadata": {
"trusted": true
},
"outputs": [],
"execution_count": 67
},
{
"id": "159e2035-7199-486c-bd13-a1dced3cb4a4",
"cell_type": "code",
"source": "from shapely.wkt import loads as loads_wkt\nfrom functools import partial\nfrom pyproj import Proj, Transformer\nfrom shapely.geometry import Point\nfrom shapely.ops import transform\n\n# Copied from https://racum.blog/articles/projection-shapely/\ndef get_projections(anchor_point=None):\n if not anchor_point: # If no anchor, set 0/0 (normal Mercator).\n anchor_point = Point(0, 0)\n crs = {\n 'proj': 'omerc',\n 'lat_0': anchor_point.y,\n 'lonc': anchor_point.x,\n 'alpha': 1e-6,\n 'k': 1,\n 'gamma': 0.0,\n 'units': 'm',\n 'ellps': 'WGS84',\n 'no_defs': True,\n }\n proj = Proj(crs, preserve_units=True)\n proj_inverse = Transformer.from_proj(proj, 'EPSG:4326', always_xy=True).transform\n return partial(transform, proj), partial(transform, proj_inverse)\n\ntotal_area_meters = 0\n\nfor idx, location in enumerate(all_locations):\n if location.get('attributes')['geometry']:\n geom = loads_wkt(location.get('attributes')['geometry']['value'])\n geodesic_to_cartesian, _ = get_projections(geom.centroid)\n geom_projected = geodesic_to_cartesian(geom)\n total_area_meters += geom_projected.area\n\nprint(total_area_meters)",
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "10843.147495371248\n"
}
],
"execution_count": 69
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment