chrisli124 commited on
Commit
03ea002
Β·
1 Parent(s): ebae247

convert geojson polygons to points

Browse files
__pycache__/app.cpython-311.pyc CHANGED
Binary files a/__pycache__/app.cpython-311.pyc and b/__pycache__/app.cpython-311.pyc differ
 
app.py CHANGED
@@ -19,9 +19,8 @@ from datetime import datetime
19
  from helpers.fetch_data import fetch_data
20
  from helpers.residuals import get_residual_plot
21
  from helpers.reduce_precision import reduce_coordinate_precision
22
- from shapely.geometry import shape
23
- from shapely.geometry import mapping
24
- from shapely.geometry import Polygon as ShapelyPolygon
25
  import io
26
 
27
  # ------------------------------
@@ -48,21 +47,40 @@ residual_data = pd.read_csv('data/residual_by_country.csv')
48
  selected_map = reactive.Value(None)
49
 
50
  #load band 1 data by default
51
- with open('data/band_1.geojson') as w:
52
  band_1_data = json.load(w)
53
  band_1_data = reduce_coordinate_precision(band_1_data, precision=5) #reduce precision to aid in rendering
54
 
 
55
  IWI_values = [feature['properties']['IWI']
56
  for feature in band_1_data['features']]
57
- norm = colors.Normalize(vmin=min(IWI_values), vmax=max(IWI_values))
58
- colormap = cm.get_cmap("plasma")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
 
 
 
60
 
 
61
  def get_color(iwi):
 
62
  rgba = colormap(norm(iwi)) # Convert to RGBA
63
  return colors.to_hex(rgba) # Convert to HEX
64
 
65
-
66
  # Function to clean up out-of-range values and get values
67
  def get_clean_values(src, band_idx=1):
68
  band_data = src.read(band_idx)
@@ -400,10 +418,17 @@ def server(input, output, session):
400
  }
401
 
402
  band_1_json = GeoJSON(data=band_1_data,
403
- style={'radius': 2, 'opacity': 0.8, 'weight': 1.9},
404
- point_style={'radius': 3},
405
  name='Release'
406
  )
 
 
 
 
 
 
 
407
  m.add_layer(band_1_json)
408
 
409
  geo_json = GeoJSON(
 
19
  from helpers.fetch_data import fetch_data
20
  from helpers.residuals import get_residual_plot
21
  from helpers.reduce_precision import reduce_coordinate_precision
22
+ from shapely import LineString, Polygon
23
+ from matplotlib.colors import BoundaryNorm
 
24
  import io
25
 
26
  # ------------------------------
 
47
  selected_map = reactive.Value(None)
48
 
49
  #load band 1 data by default
50
+ with open('data/simplified_band_1.geojson') as w:
51
  band_1_data = json.load(w)
52
  band_1_data = reduce_coordinate_precision(band_1_data, precision=5) #reduce precision to aid in rendering
53
 
54
+
55
  IWI_values = [feature['properties']['IWI']
56
  for feature in band_1_data['features']]
57
+ # Define IWI value bins and their corresponding color ranges
58
+ iwi_bins = [0.052, 0.140, 0.161, 0.187, 0.240, 0.696] # Custom bin values
59
+ iwi_labels = [
60
+ "0.052 – 0.140",
61
+ "0.140 – 0.161",
62
+ "0.161 – 0.187",
63
+ "0.187 – 0.240",
64
+ "0.240 – 0.696"
65
+ ]
66
+ iwi_mappings = {
67
+ "0.052 – 0.140": "#0d0887",
68
+ "0.140 – 0.161": "#7d03a8",
69
+ "0.161 – 0.187": "#cb4679",
70
+ "0.187 – 0.240": "#f89441",
71
+ "0.240 – 0.696": "#f0f921"
72
+ }
73
 
74
+ # Generate a colormap and norm based on the custom bins
75
+ colormap = cm.get_cmap("plasma") # Choose your colormap
76
+ norm = BoundaryNorm(iwi_bins, colormap.N)
77
 
78
+ # Function to get color based on IWI value
79
  def get_color(iwi):
80
+ # Find the color for the given IWI value based on the colormap and norm
81
  rgba = colormap(norm(iwi)) # Convert to RGBA
82
  return colors.to_hex(rgba) # Convert to HEX
83
 
 
84
  # Function to clean up out-of-range values and get values
85
  def get_clean_values(src, band_idx=1):
86
  band_data = src.read(band_idx)
 
418
  }
419
 
420
  band_1_json = GeoJSON(data=band_1_data,
421
+ style={'radius': 0.05, 'opacity': 0.8, 'weight': 0.5},
422
+ point_style={'radius': 0.05},
423
  name='Release'
424
  )
425
+ legend = LegendControl(iwi_mappings,
426
+ position="bottomleft",
427
+ title="IWI Values",
428
+ )
429
+
430
+ # Add the legend to the map
431
+ m.add_control(legend)
432
  m.add_layer(band_1_json)
433
 
434
  geo_json = GeoJSON(
data/simplified_band_1.geojson ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:83cfb95ccb32b9974d5539e19c136eacdfceaf3e317b8b9df1726a4c707db2c6
3
+ size 44034863
notebooks/simplify_polygon.ipynb ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 3,
6
+ "id": "81ec2c58",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "data": {
11
+ "text/plain": [
12
+ "[{'type': 'Feature',\n",
13
+ " 'properties': {'IWI': 0.2388916015625},\n",
14
+ " 'geometry': {'type': 'Polygon',\n",
15
+ " 'coordinates': [[[9.63254496008522, 37.40584843073688],\n",
16
+ " [9.63254496008522, 37.34548164364405],\n",
17
+ " [9.692911747178051, 37.34548164364405],\n",
18
+ " [9.692911747178051, 37.40584843073688],\n",
19
+ " [9.63254496008522, 37.40584843073688]]]}},\n",
20
+ " {'type': 'Feature',\n",
21
+ " 'properties': {'IWI': 0.21142578125},\n",
22
+ " 'geometry': {'type': 'Polygon',\n",
23
+ " 'coordinates': [[[9.451444598806724, 37.34548164364405],\n",
24
+ " [9.451444598806724, 37.28511485655122],\n",
25
+ " [9.511811385899556, 37.28511485655122],\n",
26
+ " [9.511811385899556, 37.34548164364405],\n",
27
+ " [9.451444598806724, 37.34548164364405]]]}},\n",
28
+ " {'type': 'Feature',\n",
29
+ " 'properties': {'IWI': 0.191650390625},\n",
30
+ " 'geometry': {'type': 'Polygon',\n",
31
+ " 'coordinates': [[[9.511811385899556, 37.34548164364405],\n",
32
+ " [9.511811385899556, 37.28511485655122],\n",
33
+ " [9.572178172992388, 37.28511485655122],\n",
34
+ " [9.572178172992388, 37.34548164364405],\n",
35
+ " [9.511811385899556, 37.34548164364405]]]}},\n",
36
+ " {'type': 'Feature',\n",
37
+ " 'properties': {'IWI': 0.1314697265625},\n",
38
+ " 'geometry': {'type': 'Polygon',\n",
39
+ " 'coordinates': [[[9.572178172992388, 37.34548164364405],\n",
40
+ " [9.572178172992388, 37.28511485655122],\n",
41
+ " [9.63254496008522, 37.28511485655122],\n",
42
+ " [9.63254496008522, 37.34548164364405],\n",
43
+ " [9.572178172992388, 37.34548164364405]]]}},\n",
44
+ " {'type': 'Feature',\n",
45
+ " 'properties': {'IWI': 0.169921875},\n",
46
+ " 'geometry': {'type': 'Polygon',\n",
47
+ " 'coordinates': [[[9.63254496008522, 37.34548164364405],\n",
48
+ " [9.63254496008522, 37.28511485655122],\n",
49
+ " [9.692911747178051, 37.28511485655122],\n",
50
+ " [9.692911747178051, 37.34548164364405],\n",
51
+ " [9.63254496008522, 37.34548164364405]]]}},\n",
52
+ " {'type': 'Feature',\n",
53
+ " 'properties': {'IWI': 0.261962890625},\n",
54
+ " 'geometry': {'type': 'Polygon',\n",
55
+ " 'coordinates': [[[9.692911747178051, 37.34548164364405],\n",
56
+ " [9.692911747178051, 37.28511485655122],\n",
57
+ " [9.75327853427088, 37.28511485655122],\n",
58
+ " [9.75327853427088, 37.34548164364405],\n",
59
+ " [9.692911747178051, 37.34548164364405]]]}},\n",
60
+ " {'type': 'Feature',\n",
61
+ " 'properties': {'IWI': 0.355712890625},\n",
62
+ " 'geometry': {'type': 'Polygon',\n",
63
+ " 'coordinates': [[[9.75327853427088, 37.34548164364405],\n",
64
+ " [9.75327853427088, 37.28511485655122],\n",
65
+ " [9.813645321363712, 37.28511485655122],\n",
66
+ " [9.813645321363712, 37.34548164364405],\n",
67
+ " [9.75327853427088, 37.34548164364405]]]}},\n",
68
+ " {'type': 'Feature',\n",
69
+ " 'properties': {'IWI': 0.36572265625},\n",
70
+ " 'geometry': {'type': 'Polygon',\n",
71
+ " 'coordinates': [[[9.813645321363712, 37.34548164364405],\n",
72
+ " [9.813645321363712, 37.28511485655122],\n",
73
+ " [9.874012108456544, 37.28511485655122],\n",
74
+ " [9.874012108456544, 37.34548164364405],\n",
75
+ " [9.813645321363712, 37.34548164364405]]]}},\n",
76
+ " {'type': 'Feature',\n",
77
+ " 'properties': {'IWI': 0.215087890625},\n",
78
+ " 'geometry': {'type': 'Polygon',\n",
79
+ " 'coordinates': [[[9.270344237528228, 37.28511485655122],\n",
80
+ " [9.270344237528228, 37.224748069458386],\n",
81
+ " [9.33071102462106, 37.224748069458386],\n",
82
+ " [9.33071102462106, 37.28511485655122],\n",
83
+ " [9.270344237528228, 37.28511485655122]]]}},\n",
84
+ " {'type': 'Feature',\n",
85
+ " 'properties': {'IWI': 0.145263671875},\n",
86
+ " 'geometry': {'type': 'Polygon',\n",
87
+ " 'coordinates': [[[9.33071102462106, 37.28511485655122],\n",
88
+ " [9.33071102462106, 37.224748069458386],\n",
89
+ " [9.391077811713892, 37.224748069458386],\n",
90
+ " [9.391077811713892, 37.28511485655122],\n",
91
+ " [9.33071102462106, 37.28511485655122]]]}}]"
92
+ ]
93
+ },
94
+ "execution_count": 3,
95
+ "metadata": {},
96
+ "output_type": "execute_result"
97
+ }
98
+ ],
99
+ "source": [
100
+ "from shapely.geometry import shape\n",
101
+ "from shapely.geometry import mapping\n",
102
+ "import json \n",
103
+ "\n",
104
+ "with open('data/band_1.geojson') as a: \n",
105
+ " geojson_data = json.load(a)\n",
106
+ "\n",
107
+ "\n",
108
+ "geojson_data['features'][:10]"
109
+ ]
110
+ },
111
+ {
112
+ "cell_type": "code",
113
+ "execution_count": 14,
114
+ "id": "5720f337",
115
+ "metadata": {},
116
+ "outputs": [],
117
+ "source": [
118
+ "# Simplify a geometry\n",
119
+ "from shapely.geometry import shape, Point\n",
120
+ "def simplify_geometry_to_centroid(geometry):\n",
121
+ " geom = shape(geometry)\n",
122
+ " centroid = geom.centroid # Calculate the centroid of the polygon\n",
123
+ " return mapping(Point(centroid.x, centroid.y)) # Convert centroid to Point geometry\n",
124
+ "\n",
125
+ "# Apply simplification to each feature in your GeoJSON\n",
126
+ "for feature in geojson_data['features']:\n",
127
+ " feature['geometry'] = simplify_geometry_to_centroid(feature['geometry'])\n",
128
+ "\n",
129
+ "with open('data/simplified_band_1.geojson', 'w') as w:\n",
130
+ " json.dump(geojson_data, w, indent=4)"
131
+ ]
132
+ },
133
+ {
134
+ "cell_type": "code",
135
+ "execution_count": 19,
136
+ "id": "402ae676",
137
+ "metadata": {},
138
+ "outputs": [
139
+ {
140
+ "name": "stdout",
141
+ "output_type": "stream",
142
+ "text": [
143
+ "#0d0887\n",
144
+ "#7d03a8\n",
145
+ "#cb4679\n",
146
+ "#f89441\n",
147
+ "#f0f921\n"
148
+ ]
149
+ },
150
+ {
151
+ "name": "stderr",
152
+ "output_type": "stream",
153
+ "text": [
154
+ "/var/folders/fd/f53d2xmj7rv2ks6v0c5q59880000gn/T/ipykernel_27622/1044773481.py:15: MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 and will be removed two minor releases later. Use ``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap(obj)`` instead.\n",
155
+ " colormap = cm.get_cmap(\"plasma\") # Choose your colormap\n"
156
+ ]
157
+ }
158
+ ],
159
+ "source": [
160
+ "import matplotlib.cm as cm\n",
161
+ "from matplotlib.colors import BoundaryNorm\n",
162
+ "import matplotlib.colors as colors\n",
163
+ "\n",
164
+ "iwi_bins = [0.052, 0.140, 0.161, 0.187, 0.240, 0.696] # Custom bin values\n",
165
+ "iwi_labels = [\n",
166
+ " \"0.052 – 0.140\",\n",
167
+ " \"0.140 – 0.161\",\n",
168
+ " \"0.161 – 0.187\",\n",
169
+ " \"0.187 – 0.240\",\n",
170
+ " \"0.240 – 0.696\"\n",
171
+ "]\n",
172
+ "\n",
173
+ "# Generate a colormap and norm based on the custom bins\n",
174
+ "colormap = cm.get_cmap(\"plasma\") # Choose your colormap\n",
175
+ "norm = BoundaryNorm(iwi_bins, colormap.N)\n",
176
+ "\n",
177
+ "# Function to get color based on IWI value\n",
178
+ "def get_color(iwi):\n",
179
+ " # Find the color for the given IWI value based on the colormap and norm\n",
180
+ " rgba = colormap(norm(iwi)) # Convert to RGBA\n",
181
+ " return colors.to_hex(rgba) # Convert to HEX\n",
182
+ "\n",
183
+ "\n",
184
+ "print(get_color(0.1))\n",
185
+ "print(get_color(0.15))\n",
186
+ "print(get_color(0.17))\n",
187
+ "print(get_color(0.22))\n",
188
+ "print(get_color(0.5))"
189
+ ]
190
+ }
191
+ ],
192
+ "metadata": {
193
+ "kernelspec": {
194
+ "display_name": "myenv",
195
+ "language": "python",
196
+ "name": "python3"
197
+ },
198
+ "language_info": {
199
+ "codemirror_mode": {
200
+ "name": "ipython",
201
+ "version": 3
202
+ },
203
+ "file_extension": ".py",
204
+ "mimetype": "text/x-python",
205
+ "name": "python",
206
+ "nbconvert_exporter": "python",
207
+ "pygments_lexer": "ipython3",
208
+ "version": "3.10.13"
209
+ }
210
+ },
211
+ "nbformat": 4,
212
+ "nbformat_minor": 5
213
+ }