{ "cells": [ { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import leafmap as leafmap\n", "import leafmap.foliumap as leaf_folium\n", "import leafmap.toolbar\n", "# import leafmap.toolbar as toolbar\n", "\n", "tmp_map = leafmap.Map()\n", "\n", "Map = leaf_folium.Map()\n", "\n", "url = \"https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Land_Cover_L48/wms?\"\n", "Map.add_wms_layer(\n", " url,\n", " layers=\"NLCD_2016_Land_Cover_L48\",\n", " name=\"NLCD 2016 CONUS Land Cover\",\n", " format=\"image/png\",\n", " transparent=True,\n", ")\n", "# Map.add_legend(builtin_legend=\"NLCD\", draggable=False)\n", "Map.sandbox_path = \"/home/patel_zeel/bhuvan\"\n", "# toolbar.tool_template(Map)\n", "\n", "Map.toolbar = tmp_map.toolbar\n", "Map" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from io import BytesIO\n", "import requests\n", "\n", "file_url = \"https://drive.google.com/file/d/1fOujaaj_hJBMJIT-EsFzg6_6I-eFCgVs/view?usp=drivesdk\"\n", "if isinstance(file_url, str):\n", " if file_url.startswith(\"https://drive.google.com/file/d/\"):\n", " ID = file_url.replace(\"https://drive.google.com/file/d/\", \"\").split(\"/\")[0]\n", " file_url = f\"https://drive.google.com/uc?id={ID}\"\n", " elif file_url.startswith(\"https://drive.google.com/open?id=\"):\n", " ID = file_url.replace(\"https://drive.google.com/open?id=\", \"\")\n", " file_url = f\"https://drive.google.com/uc?id={ID}\"\n", "\n", " response = requests.get(file_url)\n", " bytes_data = BytesIO(response.content)\n", " string_data = response.text" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import geopandas as gpd\n", "import kml2geojson\n", "if string_data.startswith(\"\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
geometrynamestyleUrl
0POINT Z (73.90178 20.91581 0)1#m_ylw-pushpin
1POINT Z (73.89839 20.91803 0)3#m_ylw-pushpin
2POINT Z (73.89878 20.91578 0)2#m_ylw-pushpin
3POINT Z (73.90075 20.91961 0)4#m_ylw-pushpin
4POLYGON Z ((73.89878 20.91579 0, 73.90179 20.9...Polygon Measure#inline1
\n", "" ], "text/plain": [ " geometry name \\\n", "0 POINT Z (73.90178 20.91581 0) 1 \n", "1 POINT Z (73.89839 20.91803 0) 3 \n", "2 POINT Z (73.89878 20.91578 0) 2 \n", "3 POINT Z (73.90075 20.91961 0) 4 \n", "4 POLYGON Z ((73.89878 20.91579 0, 73.90179 20.9... Polygon Measure \n", "\n", " styleUrl \n", "0 #m_ylw-pushpin \n", "1 #m_ylw-pushpin \n", "2 #m_ylw-pushpin \n", "3 #m_ylw-pushpin \n", "4 #inline1 " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "input_gdf" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from shapely.ops import transform\n", "\n", "def shape_3d_to_2d(shape):\n", " if shape.has_z:\n", " return transform(lambda x, y, z: (x, y), shape)\n", " else:\n", " return shape\n", "\n", "def preprocess_gdf(gdf):\n", " gdf = gdf.to_crs(epsg=7761) # epsg for Gujarat\n", " gdf[\"geometry\"] = gdf[\"geometry\"].apply(shape_3d_to_2d)\n", " gdf[\"geometry\"] = gdf.buffer(0) # Fixes some invalid geometries\n", " return gdf\n", "input_gdf = preprocess_gdf(input_gdf)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
geometrynamestyleUrl
0POLYGON EMPTY1#m_ylw-pushpin
1POLYGON EMPTY3#m_ylw-pushpin
2POLYGON EMPTY2#m_ylw-pushpin
3POLYGON EMPTY4#m_ylw-pushpin
4POLYGON ((1262518.164 840333.056, 1262472.46 8...Polygon Measure#inline1
\n", "
" ], "text/plain": [ " geometry name \\\n", "0 POLYGON EMPTY 1 \n", "1 POLYGON EMPTY 3 \n", "2 POLYGON EMPTY 2 \n", "3 POLYGON EMPTY 4 \n", "4 POLYGON ((1262518.164 840333.056, 1262472.46 8... Polygon Measure \n", "\n", " styleUrl \n", "0 #m_ylw-pushpin \n", "1 #m_ylw-pushpin \n", "2 #m_ylw-pushpin \n", "3 #m_ylw-pushpin \n", "4 #inline1 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "input_gdf" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_850691/2543908025.py:3: ShapelyDeprecationWarning: The 'type' attribute is deprecated, and will be removed in the future. You can use the 'geom_type' attribute instead.\n", " return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n", "/tmp/ipykernel_850691/2543908025.py:3: ShapelyDeprecationWarning: The 'type' attribute is deprecated, and will be removed in the future. You can use the 'geom_type' attribute instead.\n", " return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n", "/tmp/ipykernel_850691/2543908025.py:3: ShapelyDeprecationWarning: The 'type' attribute is deprecated, and will be removed in the future. You can use the 'geom_type' attribute instead.\n", " return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n", "/tmp/ipykernel_850691/2543908025.py:3: ShapelyDeprecationWarning: The 'type' attribute is deprecated, and will be removed in the future. You can use the 'geom_type' attribute instead.\n", " return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n", "/tmp/ipykernel_850691/2543908025.py:3: ShapelyDeprecationWarning: The 'type' attribute is deprecated, and will be removed in the future. You can use the 'geom_type' attribute instead.\n", " return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n" ] } ], "source": [ "def is_valid_polygon(geometry_gdf):\n", " geometry = geometry_gdf.geometry.item()\n", " return (geometry.type == \"Polygon\") and (not geometry.is_empty)\n", "\n", "for i in range(len(input_gdf)):\n", " geometry_gdf = input_gdf[input_gdf.index == i]\n", " if is_valid_polygon(geometry_gdf):\n", " break" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
geometrynamestyleUrl
4POLYGON ((1262518.164 840333.056, 1262472.46 8...Polygon Measure#inline1
\n", "
" ], "text/plain": [ " geometry name \\\n", "4 POLYGON ((1262518.164 840333.056, 1262472.46 8... Polygon Measure \n", "\n", " styleUrl \n", "4 #inline1 " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "geometry_gdf" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import geopandas as gpd\n", "from tqdm.notebook import tqdm\n", "from joblib import Parallel, delayed" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "e90c443ceec74b3e9bf223587db76898", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/15 [00:00