Skip to content

Commit

Permalink
fix: change voronoi example notebook (#159)
Browse files Browse the repository at this point in the history
* fix: added wgs84 const
  • Loading branch information
RaczeQ authored Jan 13, 2023
1 parent f94a37c commit 0882609
Showing 1 changed file with 42 additions and 34 deletions.
76 changes: 42 additions & 34 deletions examples/regionizers/voronoi_regionizer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"import plotly.express as px\n",
"from shapely.geometry import MultiPolygon, Point, Polygon\n",
"\n",
"from srai.regionizers import VoronoiRegionizer"
"from srai.regionizers import VoronoiRegionizer\n",
"from srai.utils.constants import WGS84_CRS"
]
},
{
Expand Down Expand Up @@ -43,7 +44,7 @@
" ]\n",
" },\n",
" index=[1, 2, 3, 4, 5, 6],\n",
" crs=\"EPSG:4326\",\n",
" crs=WGS84_CRS,\n",
")"
]
},
Expand Down Expand Up @@ -102,7 +103,10 @@
"fig.update_traces(marker_color=\"white\", marker_size=10, selector=dict(type=\"scattergeo\"))\n",
"fig.update_layout(coloraxis_showscale=False)\n",
"fig.update_geos(\n",
" projection_type=\"orthographic\", projection_rotation_lon=20, projection_rotation_lat=30\n",
" projection_type=\"orthographic\",\n",
" projection_rotation_lon=20,\n",
" projection_rotation_lat=30,\n",
" showlakes=False,\n",
")\n",
"fig.update_layout(height=800, width=800, margin={\"r\": 0, \"t\": 0, \"l\": 0, \"b\": 0})\n",
"fig.show(renderer=\"png\") # replace with fig.show() to allow interactivity"
Expand Down Expand Up @@ -144,25 +148,36 @@
"metadata": {},
"outputs": [],
"source": [
"N_POINTS = 100\n",
"# generate some random points within the bounds\n",
"minx, miny, maxx, maxy = uk_gdf_shape.bounds\n",
"def generate_random_points(shape, n_points=100):\n",
" minx, miny, maxx, maxy = shape.bounds\n",
" pts = []\n",
"\n",
" while len(pts) < 4:\n",
" randx = np.random.uniform(minx, maxx, n_points)\n",
" randy = np.random.uniform(miny, maxy, n_points)\n",
" coords = np.vstack((randx, randy)).T\n",
"\n",
"randx = np.random.uniform(minx, maxx, N_POINTS)\n",
"randy = np.random.uniform(miny, maxy, N_POINTS)\n",
"coords = np.vstack((randx, randy)).T\n",
" # use only the points inside the geographic area\n",
" pts = [p for p in list(map(Point, coords)) if p.within(shape)]\n",
"\n",
"# use only the points inside the geographic area\n",
" del coords # not used any more\n",
"\n",
"pts = [p for p in list(map(Point, coords)) if p.within(uk_gdf_shape)]\n",
" return pts"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pts = generate_random_points(uk_gdf_shape)\n",
"\n",
"uk_seeds_gdf = gpd.GeoDataFrame(\n",
" {\"geometry\": pts},\n",
" index=list(range(len(pts))),\n",
" crs=\"EPSG:4326\",\n",
")\n",
"\n",
"del coords # not used any more"
" crs=WGS84_CRS,\n",
")"
]
},
{
Expand Down Expand Up @@ -219,6 +234,7 @@
"fig.add_trace(fig2.data[0])\n",
"fig.update_traces(marker_color=\"black\", marker_size=6, selector=dict(type=\"scattergeo\"))\n",
"fig.update_layout(coloraxis_showscale=False)\n",
"minx, miny, maxx, maxy = uk_gdf_shape.bounds\n",
"fig.update_geos(\n",
" projection_type=\"natural earth\",\n",
" lataxis_range=[miny - 1, maxy + 1],\n",
Expand Down Expand Up @@ -271,7 +287,7 @@
" stations.append({\"id\": idx, \"geometry\": Point(*pos)})\n",
" positions.add(pos)\n",
"\n",
"stations_gdf = gpd.GeoDataFrame(data=stations, crs=\"EPSG:4326\").set_index(\"id\")\n",
"stations_gdf = gpd.GeoDataFrame(data=stations, crs=WGS84_CRS).set_index(\"id\")\n",
"\n",
"del stations_csv\n",
"del stations\n",
Expand Down Expand Up @@ -405,25 +421,13 @@
"metadata": {},
"outputs": [],
"source": [
"N_POINTS = 100\n",
"# generate some random points within the bounds\n",
"minx, miny, maxx, maxy = pl_gdf_shape.bounds\n",
"\n",
"randx = np.random.uniform(minx, maxx, N_POINTS)\n",
"randy = np.random.uniform(miny, maxy, N_POINTS)\n",
"coords = np.vstack((randx, randy)).T\n",
"\n",
"# use only the points inside the geographic area\n",
"\n",
"pts = [p for p in list(map(Point, coords)) if p.within(pl_gdf_shape)]\n",
"pts = generate_random_points(pl_gdf_shape)\n",
"\n",
"pl_seeds_gdf = gpd.GeoDataFrame(\n",
" {\"geometry\": pts},\n",
" index=list(range(len(pts))),\n",
" crs=\"EPSG:4326\",\n",
")\n",
"\n",
"del coords"
" crs=WGS84_CRS,\n",
")"
]
},
{
Expand Down Expand Up @@ -453,7 +457,7 @@
"pl_regions_2d_gdf = gpd.GeoDataFrame(\n",
" {\"geometry\": [orient_geom(geom) for geom in region_polys.values()]},\n",
" index=list(range(len(region_polys))),\n",
" crs=\"EPSG:4326\",\n",
" crs=WGS84_CRS,\n",
")"
]
},
Expand Down Expand Up @@ -536,6 +540,8 @@
" for c in [1, 2]:\n",
" fig.add_trace(points_plot.data[0], row=r, col=c)\n",
"\n",
"minx, miny, maxx, maxy = pl_gdf_shape.bounds\n",
"\n",
"fig.update_traces(marker_color=\"black\", marker_size=6, selector=dict(type=\"scattergeo\"))\n",
"fig.update_layout(coloraxis_showscale=False)\n",
"fig.update_geos(\n",
Expand All @@ -544,6 +550,7 @@
" lonaxis_range=[minx - 1, maxx + 1],\n",
" resolution=50,\n",
" row=1,\n",
" showlakes=False,\n",
")\n",
"\n",
"fig.update_geos(\n",
Expand All @@ -552,6 +559,7 @@
" lonaxis_range=[minx + 2, maxx - 2],\n",
" resolution=50,\n",
" row=2,\n",
" showlakes=False,\n",
")\n",
"\n",
"fig.update_traces(marker={\"opacity\": 0.6}, selector=dict(type=\"choropleth\"), row=1)\n",
Expand All @@ -569,7 +577,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.10 ('.venv': venv)",
"display_name": ".venv",
"language": "python",
"name": "python3"
},
Expand All @@ -587,7 +595,7 @@
},
"vscode": {
"interpreter": {
"hash": "27588098aedc4e0690c9f009d86804b277352852ed19dd471efc5e9817d4b199"
"hash": "4153976b658cb8b76d04b10dc7a0c871c2dac1d3dcfe690ad61d83a61969a12e"
}
}
},
Expand Down

0 comments on commit 0882609

Please sign in to comment.