{ "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 }