Spaces:
Sleeping
Sleeping
File size: 3,922 Bytes
9283e79 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"id": "81ec2c58",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"65673.0\n"
]
}
],
"source": [
"from shapely.geometry import shape\n",
"from shapely.geometry import mapping\n",
"import json \n",
"\n",
"with open('data/band_1.geojson') as a: \n",
" geojson_data = json.load(a)\n",
"\n",
"\n",
"num_points = len(geojson_data['features'])/2\n",
"print(num_points)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "5720f337",
"metadata": {},
"outputs": [],
"source": [
"# Simplify a geometry\n",
"from shapely.geometry import shape, Point\n",
"import random \n",
"def simplify_geometry_to_centroid(geometry):\n",
" geom = shape(geometry)\n",
" centroid = geom.centroid # Calculate the centroid of the polygon\n",
" return mapping(Point(centroid.x, centroid.y)) # Convert centroid to Point geometry\n",
"\n",
"# Apply simplification to each feature in your GeoJSON\n",
"for feature in geojson_data['features']:\n",
" feature['geometry'] = simplify_geometry_to_centroid(feature['geometry'])\n",
"\n",
"#shuffle data \n",
"random.shuffle(geojson_data['features'])\n",
"#only sample half of the data \n",
"geojson_data['features'] = geojson_data['features'][:int(num_points)]\n",
"\n",
"with open('data/simplified_band_1.geojson', 'w') as w:\n",
" json.dump(geojson_data, w, indent=4)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "402ae676",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"#0d0887\n",
"#7d03a8\n",
"#cb4679\n",
"#f89441\n",
"#f0f921\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/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",
" colormap = cm.get_cmap(\"plasma\") # Choose your colormap\n"
]
}
],
"source": [
"import matplotlib.cm as cm\n",
"from matplotlib.colors import BoundaryNorm\n",
"import matplotlib.colors as colors\n",
"\n",
"iwi_bins = [0.052, 0.140, 0.161, 0.187, 0.240, 0.696] # Custom bin values\n",
"iwi_labels = [\n",
" \"0.052 β 0.140\",\n",
" \"0.140 β 0.161\",\n",
" \"0.161 β 0.187\",\n",
" \"0.187 β 0.240\",\n",
" \"0.240 β 0.696\"\n",
"]\n",
"\n",
"# Generate a colormap and norm based on the custom bins\n",
"colormap = cm.get_cmap(\"plasma\") # Choose your colormap\n",
"norm = BoundaryNorm(iwi_bins, colormap.N)\n",
"\n",
"# Function to get color based on IWI value\n",
"def get_color(iwi):\n",
" # Find the color for the given IWI value based on the colormap and norm\n",
" rgba = colormap(norm(iwi)) # Convert to RGBA\n",
" return colors.to_hex(rgba) # Convert to HEX\n",
"\n",
"\n",
"print(get_color(0.1))\n",
"print(get_color(0.15))\n",
"print(get_color(0.17))\n",
"print(get_color(0.22))\n",
"print(get_color(0.5))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "myenv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|