Skip to content

Commit d528284

Browse files
committed
now clip uses earthpy which is more reliable
1 parent adee2b0 commit d528284

2 files changed

Lines changed: 67 additions & 131 deletions

File tree

mapping_utils/notebooks/ClipGeoPackageWithExternalGeometry.ipynb

Lines changed: 67 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# PLEASE NOTE: multipolygon and in general multy-geometries are not supported!"
8+
]
9+
},
310
{
411
"cell_type": "code",
5-
"execution_count": 38,
12+
"execution_count": 17,
613
"metadata": {},
714
"outputs": [
815
{
@@ -29,11 +36,12 @@
2936
},
3037
{
3138
"cell_type": "code",
32-
"execution_count": 39,
39+
"execution_count": 18,
3340
"metadata": {},
3441
"outputs": [],
3542
"source": [
36-
"from external_code import clip_shp"
43+
"# from external_code import clip_shp\n",
44+
"from earthpy.clip import clip_shp"
3745
]
3846
},
3947
{
@@ -65,22 +73,23 @@
6573
},
6674
{
6775
"cell_type": "code",
68-
"execution_count": 40,
76+
"execution_count": 19,
6977
"metadata": {},
7078
"outputs": [
7179
{
7280
"name": "stdout",
7381
"output_type": "stream",
7482
"text": [
75-
"['Crommelin_v2 Aeolian_Deposits', 'Crommelin_v2 Craters_Outlines', 'Crommelin_v2 Faults', 'Crommelin_v2 Geologic_Contacts', 'Crommelin_v2 Map_Boundaries', 'Crommelin_v2 Radial_Indurated_Ridges', 'Crommelin_v2 Surrounding_terrains', 'Crommelin_v2 Units_Central_Crater_Bulge', 'Crommelin_v2 Units_Crater_Rim', 'Crommelin_v2 Units_Inner_Crater_Layered', 'Crommelin_v2 Units_Secondary_Craters_Related', 'Crommelin_v2 Yardang_Orientations']\n"
83+
"[' Contacts', ' Geological_UnitsOk', ' Liear_Features', ' Linear_Features', ' Pyroclastic_Deposits']\n"
7684
]
7785
}
7886
],
7987
"source": [
80-
"pack = \"/data/crom_to_clip/Crommelin_v3.gpkg\"\n",
81-
"shape = \"/data/crom_to_clip/Map_Extent/Map_Extent.shp\"\n",
82-
"\n",
88+
"pack = \"/home/luca/Downloads/Copernicus.gpkg\"\n",
89+
"shape = \"/home/luca/Downloads/Archivio/area_detail.shp\"\n",
8390
"\n",
91+
"outpack = pack\n",
92+
"outpack = \"/home/luca/Downloads/Copernicus2.gpkg\"\n",
8493
"\n",
8594
"conn = ogr.Open(pack)\n",
8695
"\n",
@@ -89,7 +98,7 @@
8998
"# next we do some filtering, not always needed.\n",
9099
"for i in conn:\n",
91100
" name = i.GetName()\n",
92-
" if name.startswith(\"Cro\"):\n",
101+
" if not (name.startswith(\" Landf\") ):\n",
93102
" layernames.append(name)\n",
94103
"\n",
95104
"if \"layer_styles\" in layernames:\n",
@@ -107,7 +116,7 @@
107116
},
108117
{
109118
"cell_type": "code",
110-
"execution_count": 41,
119+
"execution_count": 20,
111120
"metadata": {},
112121
"outputs": [
113122
{
@@ -131,26 +140,33 @@
131140
" <thead>\n",
132141
" <tr style=\"text-align: right;\">\n",
133142
" <th></th>\n",
134-
" <th>Id</th>\n",
143+
" <th>OBJECTID</th>\n",
144+
" <th>SHAPE_Leng</th>\n",
145+
" <th>SHAPE_Area</th>\n",
135146
" <th>geometry</th>\n",
136147
" </tr>\n",
137148
" </thead>\n",
138149
" <tbody>\n",
139150
" <tr>\n",
140151
" <th>0</th>\n",
141-
" <td>0</td>\n",
142-
" <td>POLYGON ((-661021.0120456349 368957.3645695243...</td>\n",
152+
" <td>1.0</td>\n",
153+
" <td>249495.769968</td>\n",
154+
" <td>3.886445e+09</td>\n",
155+
" <td>POLYGON ((-658858.2790000001 342997.0221000002...</td>\n",
143156
" </tr>\n",
144157
" </tbody>\n",
145158
"</table>\n",
146159
"</div>"
147160
],
148161
"text/plain": [
149-
" Id geometry\n",
150-
"0 0 POLYGON ((-661021.0120456349 368957.3645695243..."
162+
" OBJECTID SHAPE_Leng SHAPE_Area \\\n",
163+
"0 1.0 249495.769968 3.886445e+09 \n",
164+
"\n",
165+
" geometry \n",
166+
"0 POLYGON ((-658858.2790000001 342997.0221000002... "
151167
]
152168
},
153-
"execution_count": 41,
169+
"execution_count": 20,
154170
"metadata": {},
155171
"output_type": "execute_result"
156172
}
@@ -167,63 +183,36 @@
167183
},
168184
{
169185
"cell_type": "code",
170-
"execution_count": 42,
186+
"execution_count": 22,
171187
"metadata": {},
172188
"outputs": [
173189
{
174-
"data": {
175-
"text/plain": [
176-
"['Crommelin_v2 Aeolian_Deposits',\n",
177-
" 'Crommelin_v2 Craters_Outlines',\n",
178-
" 'Crommelin_v2 Faults',\n",
179-
" 'Crommelin_v2 Geologic_Contacts',\n",
180-
" 'Crommelin_v2 Map_Boundaries',\n",
181-
" 'Crommelin_v2 Radial_Indurated_Ridges',\n",
182-
" 'Crommelin_v2 Surrounding_terrains',\n",
183-
" 'Crommelin_v2 Units_Central_Crater_Bulge',\n",
184-
" 'Crommelin_v2 Units_Crater_Rim',\n",
185-
" 'Crommelin_v2 Units_Inner_Crater_Layered',\n",
186-
" 'Crommelin_v2 Units_Secondary_Craters_Related',\n",
187-
" 'Crommelin_v2 Yardang_Orientations']"
188-
]
189-
},
190-
"execution_count": 42,
191-
"metadata": {},
192-
"output_type": "execute_result"
193-
}
194-
],
195-
"source": [
196-
"layernames"
197-
]
198-
},
199-
{
200-
"cell_type": "code",
201-
"execution_count": null,
202-
"metadata": {},
203-
"outputs": [],
204-
"source": []
205-
},
206-
{
207-
"cell_type": "code",
208-
"execution_count": 43,
209-
"metadata": {},
210-
"outputs": [
190+
"name": "stdout",
191+
"output_type": "stream",
192+
"text": [
193+
" Contacts\n"
194+
]
195+
},
196+
{
197+
"name": "stderr",
198+
"output_type": "stream",
199+
"text": [
200+
"/run/media/luca/data/Code/planmap-notebooks-code.git/mapping_utils/notebooks/external_code.py:31: SettingWithCopyWarning: \n",
201+
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
202+
"Try using .loc[row_indexer,col_indexer] = value instead\n",
203+
"\n",
204+
"See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
205+
" shp_sub['geometry'] = shp_sub.intersection(poly)\n"
206+
]
207+
},
211208
{
212209
"name": "stdout",
213210
"output_type": "stream",
214211
"text": [
215-
"Crommelin_v2 Aeolian_Deposits\n",
216-
"Crommelin_v2 Craters_Outlines\n",
217-
"Crommelin_v2 Faults\n",
218-
"Crommelin_v2 Geologic_Contacts\n",
219-
"Crommelin_v2 Map_Boundaries\n",
220-
"Crommelin_v2 Radial_Indurated_Ridges\n",
221-
"Crommelin_v2 Surrounding_terrains\n",
222-
"Crommelin_v2 Units_Central_Crater_Bulge\n",
223-
"Crommelin_v2 Units_Crater_Rim\n",
224-
"Crommelin_v2 Units_Inner_Crater_Layered\n",
225-
"Crommelin_v2 Units_Secondary_Craters_Related\n",
226-
"Crommelin_v2 Yardang_Orientations\n"
212+
" Geological_UnitsOk\n",
213+
" Liear_Features\n",
214+
" Linear_Features\n",
215+
" Pyroclastic_Deposits\n"
227216
]
228217
}
229218
],
@@ -240,14 +229,21 @@
240229
" newtab = clip_shp(table, clipper)\n",
241230
" \n",
242231
" \n",
243-
" newname = l.split()[1]\n",
232+
" newname = l\n",
244233
" \n",
245-
" newtab.to_file(pack, driver=\"GPKG\", layer=newname)"
234+
" newtab.to_file(outpack, driver=\"GPKG\", layer=newname)"
235+
]
236+
},
237+
{
238+
"cell_type": "markdown",
239+
"metadata": {},
240+
"source": [
241+
"# if you need to apply or do something on the style table "
246242
]
247243
},
248244
{
249245
"cell_type": "code",
250-
"execution_count": 44,
246+
"execution_count": null,
251247
"metadata": {},
252248
"outputs": [],
253249
"source": [
@@ -256,22 +252,9 @@
256252
},
257253
{
258254
"cell_type": "code",
259-
"execution_count": 45,
255+
"execution_count": null,
260256
"metadata": {},
261-
"outputs": [
262-
{
263-
"ename": "IndexError",
264-
"evalue": "list index out of range",
265-
"output_type": "error",
266-
"traceback": [
267-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
268-
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
269-
"\u001b[0;32m<ipython-input-45-1e7c7ccfd115>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0morig_names\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstyles\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf_table_name\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mnew_names\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mn\u001b[0m \u001b[0;32min\u001b[0m \u001b[0morig_names\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mnew_names\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mstyles\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"f_table_name\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnew_names\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
270-
"\u001b[0;32m<ipython-input-45-1e7c7ccfd115>\u001b[0m in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0morig_names\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstyles\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf_table_name\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mnew_names\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mn\u001b[0m \u001b[0;32min\u001b[0m \u001b[0morig_names\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mnew_names\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mstyles\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"f_table_name\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnew_names\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
271-
"\u001b[0;31mIndexError\u001b[0m: list index out of range"
272-
]
273-
}
274-
],
257+
"outputs": [],
275258
"source": [
276259
"orig_names = styles.f_table_name.values\n",
277260
"new_names = [n.split()[1] for n in orig_names]\n",

mapping_utils/notebooks/external_code.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)