{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Load ROI from shapefile\n", "\n", "This example shows how to open the shapefile ( `*.shp` ) as the `easyidp.ROI` objects.\n", "\n", "
\"lotus
" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Package and data prepare\n", "\n", "Using the following code to load easyidp package and demo dataset" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import easyidp as idp\n", "\n", "test_data = idp.data.TestData()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "If you run for the first time, it will download around 400MB dataset automatically from Google Drive, please refer to [Data](../python_api/data.rst) for more details.\n", "\n", "Here are thee demo shapefiles used in this documents are:\n", "\n", "* `test_data.shp.lotus_shp` \n", "* `test_data.shp.utm53n_shp` \n", "* `test_data.shp.complex_shp` \n", "\n", "Each variable provides the path to the `*.shp` file:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "PosixPath('/Users/hwang/Library/Application Support/easyidp.data/data_for_tests/shp_test/lotus_plots.shp')" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "test_data.shp.lotus_shp" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Deal with ESPG:4326 (longitude, latitude)\n", "\n", "The `lotus_shp` used the EPGS:4326 as the Geo-projection coordinates" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[shp][proj] Use projection [WGS 84] for loaded shapefile [lotus_plots.shp]\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Read shapefile [lotus_plots.shp]: 100%|██████████| 112/112 [00:00<00:00, 1740.90it/s]\n" ] } ], "source": [ "lotus_roi = idp.ROI(test_data.shp.lotus_shp, name_field=0)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Using this method to check the CRS (geo-projection coordinate) for that shapefile:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", "Name: WGS 84\n", "Axis Info [ellipsoidal]:\n", "- Lat[north]: Geodetic latitude (degree)\n", "- Lon[east]: Geodetic longitude (degree)\n", "Area of Use:\n", "- name: World.\n", "- bounds: (-180.0, -90.0, 180.0, 90.0)\n", "Datum: World Geodetic System 1984 ensemble\n", "- Ellipsoid: WGS 84\n", "- Prime Meridian: Greenwich" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lotus_roi.crs" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "And we can check the plot polygon boundary values by:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " with 112 items\n", "[0]\tN1W1\n", "array([[139.54052962, 35.73475194],\n", " [139.54055106, 35.73475596],\n", " [139.54055592, 35.73473843],\n", " [139.54053438, 35.73473446],\n", " [139.54052962, 35.73475194]])\n", "[1]\tN1W2\n", "array([[139.54053488, 35.73473289],\n", " [139.54055632, 35.73473691],\n", " [139.54056118, 35.73471937],\n", " [139.54053963, 35.73471541],\n", " [139.54053488, 35.73473289]])\n", "...\n", "[110]\tS4E6\n", "array([[139.54090456, 35.73453742],\n", " [139.540926 , 35.73454144],\n", " [139.54093086, 35.7345239 ],\n", " [139.54090932, 35.73451994],\n", " [139.54090456, 35.73453742]])\n", "[111]\tS4E7\n", "array([[139.54090986, 35.73451856],\n", " [139.54093129, 35.73452258],\n", " [139.54093616, 35.73450504],\n", " [139.54091461, 35.73450107],\n", " [139.54090986, 35.73451856]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lotus_roi" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "Caution\n", "\n", "In the easyidp package, the ROI order is `(longitude, latitude)`, while for some other packages like `pyproj`, `shapely`, may using the `(latitude, longitude)` order, please pay attention to it when transfering between packages\n", "\n", "
" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Deal with UTM/Zone geo coordainte\n", "\n", "The `utm53_shp` using another geo coordinate other than `(longitude, latitude)`" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[shp][proj] Use projection [WGS 84 / UTM zone 53N] for loaded shapefile [lon_lat_utm53n.shp]\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Read shapefile [lon_lat_utm53n.shp]: 100%|██████████| 120/120 [00:00<00:00, 2506.63it/s]\n" ] } ], "source": [ "utm_roi = idp.ROI(test_data.shp.utm53n_shp, name_field=0)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Then we can check the CRS (geo-coordinate) of this file:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", "Name: WGS 84 / UTM zone 53N\n", "Axis Info [cartesian]:\n", "- E[east]: Easting (metre)\n", "- N[north]: Northing (metre)\n", "Area of Use:\n", "- undefined\n", "Coordinate Operation:\n", "- name: UTM zone 53N\n", "- method: Transverse Mercator\n", "Datum: World Geodetic System 1984\n", "- Ellipsoid: WGS 84\n", "- Prime Meridian: Greenwich" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "utm_roi.crs" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "And the plot polygon coordinate values:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " with 120 items\n", "[0]\t1_02\n", "array([[ 484581.96481965, 3862282.54659697],\n", " [ 484581.71899749, 3862283.05047413],\n", " [ 484581.92015662, 3862283.14994052],\n", " [ 484582.17518015, 3862282.68486107],\n", " [ 484581.96481965, 3862282.54659697]])\n", "[1]\t1_03\n", "array([[ 484583.12131417, 3862280.30678878],\n", " [ 484582.87549189, 3862280.81066591],\n", " [ 484583.07665105, 3862280.91013233],\n", " [ 484583.3316747 , 3862280.44505291],\n", " [ 484583.12131417, 3862280.30678878]])\n", "...\n", "[118]\t6_35\n", "array([[ 484589.18991156, 3862259.98283581],\n", " [ 484588.50691422, 3862261.2903283 ],\n", " [ 484588.97321497, 3862261.50689677],\n", " [ 484589.6197077 , 3862260.22275374],\n", " [ 484589.18991156, 3862259.98283581]])\n", "[119]\t6_36\n", "array([[ 484590.32812571, 3862257.73529679],\n", " [ 484589.64512804, 3862259.04278919],\n", " [ 484590.11143074, 3862259.26046666],\n", " [ 484590.75792192, 3862257.97521477],\n", " [ 484590.32812571, 3862257.73529679]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "utm_roi" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Under this coordinate, the unit is meter, and the X (the first column) is the East-West (horizontal) direction, while the Y (the second column) is the North-Sourth (vertical) direction. The axis order is the same with `(longitude, latitude)`" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Transform between CRS\n", "\n", "In some cases, for example, not the same person who prepare the DOM and shapefile, they do not share the same coordinate and have very different polygon boundary values. So they can not put together directly and need convertion." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "Caution\n", "\n", "Although the EasyIDP support the transformation between different CRS, it may have precision loss and require some computation time if the roi number is huge. \n", "\n", "It is recommended to ensure the shp and DOM/DSM/PCD share the same CRS when preparing them.\n", "\n", "
" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "For example, in the lotus case, it uses the EPSG:4326, while the DOM/DSM uses the UTM/Zone." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", "Name: WGS 84\n", "Axis Info [ellipsoidal]:\n", "- Lat[north]: Geodetic latitude (degree)\n", "- Lon[east]: Geodetic longitude (degree)\n", "Area of Use:\n", "- name: World.\n", "- bounds: (-180.0, -90.0, 180.0, 90.0)\n", "Datum: World Geodetic System 1984 ensemble\n", "- Ellipsoid: WGS 84\n", "- Prime Meridian: Greenwich" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lotus_roi.crs" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", "Name: WGS 84 / UTM zone 54N\n", "Axis Info [cartesian]:\n", "- E[east]: Easting (metre)\n", "- N[north]: Northing (metre)\n", "Area of Use:\n", "- name: Between 138°E and 144°E, northern hemisphere between equator and 84°N, onshore and offshore. Japan. Russian Federation.\n", "- bounds: (138.0, 0.0, 144.0, 84.0)\n", "Coordinate Operation:\n", "- name: UTM zone 54N\n", "- method: Transverse Mercator\n", "Datum: World Geodetic System 1984 ensemble\n", "- Ellipsoid: WGS 84\n", "- Prime Meridian: Greenwich" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dsm = idp.GeoTiff(test_data.metashape.lotus_dsm)\n", "dsm.crs" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Hence need to do the CRS transformation to match them. It is more recommended to transfer ROI because it is only the coordinate numbers, much easier to transfer than the GeoTiff are pixel matrix.\n", "\n", "Before transfer, the plot coordinate looks like this:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " with 112 items\n", "[0]\tN1W1\n", "array([[139.54052962, 35.73475194],\n", " [139.54055106, 35.73475596],\n", " [139.54055592, 35.73473843],\n", " [139.54053438, 35.73473446],\n", " [139.54052962, 35.73475194]])\n", "[1]\tN1W2\n", "array([[139.54053488, 35.73473289],\n", " [139.54055632, 35.73473691],\n", " [139.54056118, 35.73471937],\n", " [139.54053963, 35.73471541],\n", " [139.54053488, 35.73473289]])\n", "...\n", "[110]\tS4E6\n", "array([[139.54090456, 35.73453742],\n", " [139.540926 , 35.73454144],\n", " [139.54093086, 35.7345239 ],\n", " [139.54090932, 35.73451994],\n", " [139.54090456, 35.73453742]])\n", "[111]\tS4E7\n", "array([[139.54090986, 35.73451856],\n", " [139.54093129, 35.73452258],\n", " [139.54093616, 35.73450504],\n", " [139.54091461, 35.73450107],\n", " [139.54090986, 35.73451856]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lotus_roi" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "And apply the transfer: " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "lotus_roi.change_crs(dsm.crs)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Now the CRS of ROI has been changed:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", "Name: WGS 84 / UTM zone 54N\n", "Axis Info [cartesian]:\n", "- E[east]: Easting (metre)\n", "- N[north]: Northing (metre)\n", "Area of Use:\n", "- name: Between 138°E and 144°E, northern hemisphere between equator and 84°N, onshore and offshore. Japan. Russian Federation.\n", "- bounds: (138.0, 0.0, 144.0, 84.0)\n", "Coordinate Operation:\n", "- name: UTM zone 54N\n", "- method: Transverse Mercator\n", "Datum: World Geodetic System 1984 ensemble\n", "- Ellipsoid: WGS 84\n", "- Prime Meridian: Greenwich" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lotus_roi.crs" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "And also the coordinate values:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " with 112 items\n", "[0]\tN1W1\n", "array([[ 368017.7565143 , 3955511.08102276],\n", " [ 368019.70190232, 3955511.49811902],\n", " [ 368020.11263046, 3955509.54636219],\n", " [ 368018.15769062, 3955509.13563382],\n", " [ 368017.7565143 , 3955511.08102276]])\n", "[1]\tN1W2\n", "array([[ 368018.20042946, 3955508.96051697],\n", " [ 368020.14581791, 3955509.37761334],\n", " [ 368020.55654627, 3955507.42585654],\n", " [ 368018.601606 , 3955507.01512806],\n", " [ 368018.20042946, 3955508.96051697]])\n", "...\n", "[110]\tS4E6\n", "array([[ 368051.31139629, 3955486.78103425],\n", " [ 368053.25678767, 3955487.19813795],\n", " [ 368053.66752456, 3955485.24638299],\n", " [ 368051.71258131, 3955484.83564713],\n", " [ 368051.31139629, 3955486.78103425]])\n", "[111]\tS4E7\n", "array([[ 368051.75902187, 3955484.68169527],\n", " [ 368053.70441367, 3955485.09879908],\n", " [ 368054.11515079, 3955483.14704415],\n", " [ 368052.16020711, 3955482.73630818],\n", " [ 368051.75902187, 3955484.68169527]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lotus_roi" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Change the ROI labels\n", "\n", "You have must notice the `name_field` value when opening the shapefile\n", "\n", "```python\n", "lotus_roi = idp.ROI(test_data.shp.lotus_shp, name_field=0)\n", "```\n", "\n", "In this section, more details about this parameter and other controls will be introduced." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "For some shapefile, it is not encoded in `utf-8`, and the default loading may fail:\n", "\n", "```python\n", ">>> idp.shp.show_shp_fields(test_data.shp.complex_shp)\n", "---------------------------------------------------------------------------\n", "UnicodeDecodeError Traceback (most recent call last)\n", "/Users/hwang/OneDrive/Program/GitHub/EasyIDP/docs/jupyter/load_roi.ipynb Cell 35 in ()\n", "----> 1 idp.shp.show_shp_fields(test_data.shp.complex_shp)\n", "\n", "File ~/OneDrive/Program/GitHub/EasyIDP/easyidp/shp.py:114, in show_shp_fields(shp_path, encoding)\n", " 111 head = [\"[-1]\"] + [f\"[{v}] {k}\" for k, v in shp_fields.items()]\n", " 112 data = []\n", "--> 114 row_num = len(shp.records())\n", " 115 col_num = len(shp.records()[0])\n", " 117 col_align = [\"right\"] + [\"center\"] * col_num\n", "\n", "File ~/anaconda/miniconda3/envs/easyidp/lib/python3.8/site-packages/shapefile.py:1306, in Reader.records(self)\n", " 1304 f.seek(self.__dbfHdrLength)\n", " 1305 for i in range(self.numRecords):\n", "-> 1306 r = self.__record(oid=i)\n", " 1307 if r:\n", " 1308 records.append(r)\n", "\n", "File ~/anaconda/miniconda3/envs/easyidp/lib/python3.8/site-packages/shapefile.py:1281, in Reader.__record(self, oid)\n", " 1278 value = None # unknown value is set to missing\n", " 1279 else:\n", " 1280 # anything else is forced to string/unicode\n", "-> 1281 value = u(value, self.encoding, self.encodingErrors)\n", " 1282 value = value.strip()\n", "...\n", " 105 elif isinstance(v, str):\n", " 106 # Already str.\n", " 107 return v\n", "\n", "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc2 in position 2: invalid continuation byte\n", "```" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " [-1] [0] ID [1] MASSIFID [2] CROPTYPE [3] CROPDATE [4] CROPAREA [5] ATTID\n", "------ --------------------------- ------------------- -------------- -------------- -------------- -----------\n", " 0 230104112201809010000000000 2301041120000000000 小麦 2018-09-01 61525.26302\n", " 1 230104112201809010000000012 2301041120000000012 蔬菜 2018-09-01 2802.33512\n", " 2 230104112201809010000000014 2301041120000000014 玉米 2018-09-01 6960.7745\n", " ... ... ... ... ... ... ...\n", " 320 230104112201809010000000583 2301041120000000583 大豆 2018-09-01 380.41704\n", " 321 230104112201809010000000584 2301041120000000584 其它 2018-09-01 9133.25998\n", " 322 230104112201809010000000585 2301041120000000585 其它 2018-09-01 1704.27193\n" ] } ], "source": [ "idp.shp.show_shp_fields(test_data.shp.complex_shp, encoding='gbk')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "By default, if `name_field` not given, it will using the first number id as label" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/hwang/OneDrive/Program/GitHub/EasyIDP/easyidp/shp.py:399: UserWarning: Not specifying parameter 'name_field', will using the row id (from 0 to end) as the index for each polygon.Please using idp.shp.show_shp_field(shp_path) to display the full available indexs\n", " warnings.warn(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "[shp][proj] Use projection [WGS 84] for loaded shapefile [complex_shp_review.shp]\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Read shapefile [complex_shp_review.shp]: 100%|██████████| 323/323 [00:00<00:00, 64383.62it/s]\n" ] } ], "source": [ "complex_roi = idp.ROI(test_data.shp.complex_shp, encoding='gbk')" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " with 323 items\n", "[0]\t0\n", "array([[126.84383445, 45.83319255],\n", " [126.84212197, 45.83222256],\n", " [126.84142718, 45.83186291],\n", " ...,\n", " [126.84373784, 45.83328959],\n", " [126.84381378, 45.83321205],\n", " [126.84383445, 45.83319255]])\n", "[1]\t1\n", "array([[126.85042747, 45.84588275],\n", " [126.85042684, 45.84570483],\n", " [126.8504088 , 45.84570504],\n", " ...,\n", " [126.85006453, 45.84588545],\n", " [126.85033812, 45.84588368],\n", " [126.85042747, 45.84588275]])\n", "...\n", "[321]\t321\n", "array([[126.83385574, 45.84337042],\n", " [126.83385363, 45.84331501],\n", " [126.83384205, 45.8432624 ],\n", " ...,\n", " [126.83353615, 45.84359357],\n", " [126.83370583, 45.84350153],\n", " [126.83385574, 45.84337042]])\n", "[322]\t322\n", "array([[126.83056335, 45.84335627],\n", " [126.83063002, 45.84332396],\n", " [126.83067953, 45.84328632],\n", " ...,\n", " [126.83025148, 45.84341107],\n", " [126.83039799, 45.84340243],\n", " [126.83056335, 45.84335627]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex_roi" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "We can specify the 'MASSIFID' as the label instead" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[shp][proj] Use projection [WGS 84] for loaded shapefile [complex_shp_review.shp]\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Read shapefile [complex_shp_review.shp]: 100%|██████████| 323/323 [00:00<00:00, 333.24it/s]\n" ] } ], "source": [ "complex_roi = idp.ROI(test_data.shp.complex_shp, name_field='MASSIFID', encoding='gbk')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Or the equal effects by colume id:\n", "\n", "```python\n", "complex_roi = idp.ROI(test_data.shp.complex_shp, name_field=1, encoding='gbk')\n", "```\n", "\n", "Now the label has been changed:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " with 323 items\n", "[0]\t2301041120000000000\n", "array([[126.84383445, 45.83319255],\n", " [126.84212197, 45.83222256],\n", " [126.84142718, 45.83186291],\n", " ...,\n", " [126.84373784, 45.83328959],\n", " [126.84381378, 45.83321205],\n", " [126.84383445, 45.83319255]])\n", "[1]\t2301041120000000012\n", "array([[126.85042747, 45.84588275],\n", " [126.85042684, 45.84570483],\n", " [126.8504088 , 45.84570504],\n", " ...,\n", " [126.85006453, 45.84588545],\n", " [126.85033812, 45.84588368],\n", " [126.85042747, 45.84588275]])\n", "...\n", "[321]\t2301041120000000584\n", "array([[126.83385574, 45.84337042],\n", " [126.83385363, 45.84331501],\n", " [126.83384205, 45.8432624 ],\n", " ...,\n", " [126.83353615, 45.84359357],\n", " [126.83370583, 45.84350153],\n", " [126.83385574, 45.84337042]])\n", "[322]\t2301041120000000585\n", "array([[126.83056335, 45.84335627],\n", " [126.83063002, 45.84332396],\n", " [126.83067953, 45.84328632],\n", " ...,\n", " [126.83025148, 45.84341107],\n", " [126.83039799, 45.84340243],\n", " [126.83056335, 45.84335627]])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex_roi" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Or you can combine several columns together by giving a list `name_field=['CROPTYPE', 'MASSIFID']`" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[shp][proj] Use projection [WGS 84] for loaded shapefile [complex_shp_review.shp]\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Read shapefile [complex_shp_review.shp]: 100%|██████████| 323/323 [00:01<00:00, 163.54it/s]\n" ] } ], "source": [ "complex_roi = idp.ROI(test_data.shp.complex_shp, name_field=['CROPTYPE', 'MASSIFID'], encoding='gbk')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Or the equal effects by a list of colume id `name_field=[2, 1]`\n", "\n", "```python\n", "complex_roi = idp.ROI(test_data.shp.complex_shp, name_field=[2, 1], encoding='gbk')\n", "```\n", "\n", "Now the label has been changed:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " with 323 items\n", "[0]\t小麦_2301041120000000000\n", "array([[126.84383445, 45.83319255],\n", " [126.84212197, 45.83222256],\n", " [126.84142718, 45.83186291],\n", " ...,\n", " [126.84373784, 45.83328959],\n", " [126.84381378, 45.83321205],\n", " [126.84383445, 45.83319255]])\n", "[1]\t蔬菜_2301041120000000012\n", "array([[126.85042747, 45.84588275],\n", " [126.85042684, 45.84570483],\n", " [126.8504088 , 45.84570504],\n", " ...,\n", " [126.85006453, 45.84588545],\n", " [126.85033812, 45.84588368],\n", " [126.85042747, 45.84588275]])\n", "...\n", "[321]\t其它_2301041120000000584\n", "array([[126.83385574, 45.84337042],\n", " [126.83385363, 45.84331501],\n", " [126.83384205, 45.8432624 ],\n", " ...,\n", " [126.83353615, 45.84359357],\n", " [126.83370583, 45.84350153],\n", " [126.83385574, 45.84337042]])\n", "[322]\t其它_2301041120000000585\n", "array([[126.83056335, 45.84335627],\n", " [126.83063002, 45.84332396],\n", " [126.83067953, 45.84328632],\n", " ...,\n", " [126.83025148, 45.84341107],\n", " [126.83039799, 45.84340243],\n", " [126.83056335, 45.84335627]])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex_roi" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Or even add the colume title into it by giving `include_title=True`" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[shp][proj] Use projection [WGS 84] for loaded shapefile [complex_shp_review.shp]\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Read shapefile [complex_shp_review.shp]: 100%|██████████| 323/323 [00:01<00:00, 171.82it/s]\n" ] } ], "source": [ "complex_roi = idp.ROI(test_data.shp.complex_shp, name_field=['CROPTYPE', 'MASSIFID'], include_title=True, encoding='gbk')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " with 323 items\n", "[0]\tCROPTYPE_小麦_MASSIFID_2301041120000000000\n", "array([[126.84383445, 45.83319255],\n", " [126.84212197, 45.83222256],\n", " [126.84142718, 45.83186291],\n", " ...,\n", " [126.84373784, 45.83328959],\n", " [126.84381378, 45.83321205],\n", " [126.84383445, 45.83319255]])\n", "[1]\tCROPTYPE_蔬菜_MASSIFID_2301041120000000012\n", "array([[126.85042747, 45.84588275],\n", " [126.85042684, 45.84570483],\n", " [126.8504088 , 45.84570504],\n", " ...,\n", " [126.85006453, 45.84588545],\n", " [126.85033812, 45.84588368],\n", " [126.85042747, 45.84588275]])\n", "...\n", "[321]\tCROPTYPE_其它_MASSIFID_2301041120000000584\n", "array([[126.83385574, 45.84337042],\n", " [126.83385363, 45.84331501],\n", " [126.83384205, 45.8432624 ],\n", " ...,\n", " [126.83353615, 45.84359357],\n", " [126.83370583, 45.84350153],\n", " [126.83385574, 45.84337042]])\n", "[322]\tCROPTYPE_其它_MASSIFID_2301041120000000585\n", "array([[126.83056335, 45.84335627],\n", " [126.83063002, 45.84332396],\n", " [126.83067953, 45.84328632],\n", " ...,\n", " [126.83025148, 45.84341107],\n", " [126.83039799, 45.84340243],\n", " [126.83056335, 45.84335627]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex_roi" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "Caution\n", "\n", "Only the colume with unique values should be used as the ROI label, otherwise has the risk that later duplicated label overwrites previous label. The easyidp could handle such case and raise an Error.\n", "\n", "
" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", ">>> complex_roi = idp.ROI(test_data.shp.complex_shp, name_field='CROPTYPE',encoding='gbk')\n", "[shp][proj] Use projection [WGS 84] for loaded shapefile [complex_shp_review.shp]\n", "Read shapefile [complex_shp_review.shp]: 1%| | 4/323 [00:00<00:01, 221.13it/s]\n", "Output exceeds the size limit. Open the full output data in a text editor\n", "---------------------------------------------------------------------------\n", "KeyError Traceback (most recent call last)\n", "/Users/hwang/OneDrive/Program/GitHub/EasyIDP/docs/jupyter/load_roi.ipynb Cell 51 in ()\n", "----> 1 complex_roi = idp.ROI(test_data.shp.complex_shp, name_field='CROPTYPE',encoding='gbk')\n", "\n", "File ~/OneDrive/Program/GitHub/EasyIDP/easyidp/roi.py:77, in ROI.__init__(self, target_path, **kwargs)\n", " 74 self.source = target_path\n", " 76 if target_path is not None:\n", "---> 77 self.open(target_path, **kwargs)\n", "\n", "File ~/OneDrive/Program/GitHub/EasyIDP/easyidp/roi.py:136, in ROI.open(self, target_path, **kwargs)\n", " 134 ext = os.path.splitext(target_path)[-1]\n", " 135 if ext == \".shp\":\n", "--> 136 self.read_shp(target_path, **kwargs)\n", " 137 elif ext == \".json\":\n", " 138 self.read_labelme_json(target_path)\n", "\n", "File ~/OneDrive/Program/GitHub/EasyIDP/easyidp/roi.py:215, in ROI.read_shp(self, shp_path, shp_proj, name_field, include_title, encoding)\n", " 142 \"\"\"read ROI from shp file\n", " 143 \n", " 144 Parameters\n", " (...)\n", " 211 \n", " 212 \"\"\"\n", " 213 # if geotiff_proj is not None and shp_proj is not None and shp_proj.name != geotiff_proj.name:\n", "...\n", "--> 323 raise KeyError(f\"Meet with duplicated key [{plot_name}] for current shapefile, please specify another `name_field` from {shp_fields} or simple leave it blank `name_field=None`\")\n", " 325 shp_dict[plot_name] = coord_np\n", " 327 if return_proj:\n", "\n", "KeyError: \"Meet with duplicated key [玉米] for current shapefile, please specify another `name_field` from {'ID': 0, 'MASSIFID': 1, 'CROPTYPE': 2, 'CROPDATE': 3, 'CROPAREA': 4, 'ATTID': 5} or simple leave it blank `name_field=None`\"\n", "```\n" ] } ], "metadata": { "kernelspec": { "display_name": "easyidp", "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.0" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "c57a372376ecab0caeb8816769a3e0fc257b5754cdacff7b12180918199af449" } } }, "nbformat": 4, "nbformat_minor": 2 }