Joshua Sundance Bailey
commited on
Commit
·
9d62f11
1
Parent(s):
d362ca8
refactor
Browse files
geospatial-data-converter/kml_tricks.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import zipfile
|
|
|
2 |
|
3 |
import bs4
|
4 |
import fiona
|
@@ -8,122 +9,90 @@ import pandas as pd
|
|
8 |
fiona.drvsupport.supported_drivers["KML"] = "rw"
|
9 |
|
10 |
|
11 |
-
def
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
return df
|
29 |
-
|
30 |
-
|
31 |
-
def readkmz(path: str) -> gpd.GeoDataFrame:
|
32 |
-
"""Simply read kmz using geopandas/fiona without parsing Descriptions"""
|
33 |
-
# get name of kml in kmz (should be doc.kml but we don't assume)
|
34 |
with zipfile.ZipFile(path, "r") as kmz:
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
raise IndexError(
|
39 |
-
"
|
40 |
)
|
41 |
-
|
42 |
-
return gpd.read_file("zip://{}\\{
|
43 |
|
44 |
|
45 |
-
def
|
46 |
-
"""Return gpd.GeoDataFrame after reading kmz or kml and parsing Descriptions"""
|
47 |
if path.endswith(".kml"):
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
def
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
# get the KML source code as a BeautifulSoup object
|
62 |
-
soup = bs4.BeautifulSoup(kmlcode, "html.parser")
|
63 |
-
# find all rows (schemadata tags) in the soup
|
64 |
-
rowtags = soup.find_all("schemadata")
|
65 |
-
# generator expression yielding a {name: value} dict for each row
|
66 |
-
rowdicts = (
|
67 |
{field.get("name"): field.text for field in row.find_all("simpledata")}
|
68 |
-
for row in
|
69 |
)
|
70 |
-
|
71 |
-
return pd.DataFrame(
|
72 |
-
|
73 |
-
|
74 |
-
def
|
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 |
-
def simpledata_fromfile(gefile: str) -> pd.DataFrame:
|
102 |
-
"""Return DataFrame extracted from Google Earth File
|
103 |
-
parameter gefile (str): absolute or relative path to Google Earth file
|
104 |
-
(kmz or kml)
|
105 |
-
Uses simpledata tags, NOT embedded tables in feature descriptions
|
106 |
-
"""
|
107 |
-
df = simpledata_fromcode(kmlcode_fromfile(gefile))
|
108 |
-
if gefile.endswith(".kmz"):
|
109 |
-
gefile_gdf = readkmz(gefile)
|
110 |
else:
|
111 |
-
|
112 |
-
|
113 |
-
return
|
114 |
-
|
115 |
-
|
116 |
-
def
|
117 |
-
"""Extract data from Google Earth file & save as zip
|
118 |
-
parameter gefile (str): absolute or relative path to Google Earth file
|
119 |
-
parameter zipfile (str): absolute or relative path to output zip file
|
120 |
-
Will read simpledata tags OR embedded tables in feature descriptions
|
121 |
-
"""
|
122 |
-
# retrieve DataFrame from gefile and use its to_file method
|
123 |
try:
|
124 |
-
|
125 |
-
df = ge_togdf(gefile)
|
126 |
except (pd.errors.ParserError, ValueError):
|
127 |
-
|
128 |
-
df = simpledata_fromfile(gefile)
|
129 |
-
return df
|
|
|
1 |
import zipfile
|
2 |
+
from typing import Any
|
3 |
|
4 |
import bs4
|
5 |
import fiona
|
|
|
9 |
fiona.drvsupport.supported_drivers["KML"] = "rw"
|
10 |
|
11 |
|
12 |
+
def parse_description_to_gdf(gdf: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
|
13 |
+
parsed_dataframes = [
|
14 |
+
pd.read_html(desc)[1 if len(pd.read_html(desc)) > 1 else 0].T
|
15 |
+
for desc in gdf["Description"]
|
16 |
+
]
|
17 |
+
|
18 |
+
for df in parsed_dataframes:
|
19 |
+
df.columns = df.iloc[0]
|
20 |
+
df.drop(df.index[0], inplace=True)
|
21 |
+
|
22 |
+
combined_df = pd.concat(parsed_dataframes, ignore_index=True)
|
23 |
+
combined_df["geometry"] = gdf["geometry"]
|
24 |
+
|
25 |
+
return gpd.GeoDataFrame(combined_df, crs=gdf.crs)
|
26 |
+
|
27 |
+
|
28 |
+
def read_kml_file(path: str) -> Any:
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
with zipfile.ZipFile(path, "r") as kmz:
|
30 |
+
kml_files = [f for f in kmz.namelist() if f.endswith(".kml")]
|
31 |
+
|
32 |
+
if len(kml_files) != 1:
|
33 |
raise IndexError(
|
34 |
+
"KMZ contains more than one KML. Extract or convert to multiple KMLs.",
|
35 |
)
|
36 |
+
|
37 |
+
return gpd.read_file(f"zip://{path}\\{kml_files[0]}", driver="KML")
|
38 |
|
39 |
|
40 |
+
def parse_file_to_gdf(path: str) -> gpd.GeoDataFrame:
|
|
|
41 |
if path.endswith(".kml"):
|
42 |
+
return parse_description_to_gdf(gpd.read_file(path, driver="KML"))
|
43 |
+
|
44 |
+
if path.endswith(".kmz"):
|
45 |
+
return parse_description_to_gdf(read_kml_file(path))
|
46 |
+
|
47 |
+
raise ValueError("File must end with .kml or .kmz")
|
48 |
+
|
49 |
+
|
50 |
+
def extract_data_from_kml_code(kml_code: str) -> pd.DataFrame:
|
51 |
+
soup = bs4.BeautifulSoup(kml_code, "html.parser")
|
52 |
+
rows = soup.find_all("schemadata")
|
53 |
+
|
54 |
+
data = (
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
{field.get("name"): field.text for field in row.find_all("simpledata")}
|
56 |
+
for row in rows
|
57 |
)
|
58 |
+
|
59 |
+
return pd.DataFrame(data)
|
60 |
+
|
61 |
+
|
62 |
+
def extract_kml_from_file(file_path: str) -> str:
|
63 |
+
file_extension = file_path.lower().split(".")[-1]
|
64 |
+
kml_files = None
|
65 |
+
|
66 |
+
if file_extension == "kml":
|
67 |
+
with open(file_path, "r") as kml:
|
68 |
+
return kml.read()
|
69 |
+
|
70 |
+
if file_extension == "kmz":
|
71 |
+
with zipfile.ZipFile(file_path) as kmz:
|
72 |
+
kml_files = [f for f in kmz.namelist() if f.lower().endswith(".kml")]
|
73 |
+
if len(kml_files) != 1:
|
74 |
+
raise IndexError(
|
75 |
+
"KMZ contains more than one KML. Extract or convert to multiple KMLs.",
|
76 |
+
)
|
77 |
+
with kmz.open(kml_files[0]) as kml:
|
78 |
+
return kml.read().decode()
|
79 |
+
|
80 |
+
raise ValueError("File path must end with .kml or .kmz")
|
81 |
+
|
82 |
+
|
83 |
+
def extract_data_from_file(file_path: str) -> pd.DataFrame:
|
84 |
+
df = extract_data_from_kml_code(extract_kml_from_file(file_path))
|
85 |
+
|
86 |
+
if file_path.endswith(".kmz"):
|
87 |
+
file_gdf = read_kml_file(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
else:
|
89 |
+
file_gdf = gpd.read_file(file_path, driver="KML")
|
90 |
+
|
91 |
+
return gpd.GeoDataFrame(df, geometry=file_gdf["geometry"], crs=file_gdf.crs)
|
92 |
+
|
93 |
+
|
94 |
+
def read_ge_file(file_path: str) -> pd.DataFrame:
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
try:
|
96 |
+
return parse_file_to_gdf(file_path)
|
|
|
97 |
except (pd.errors.ParserError, ValueError):
|
98 |
+
return extract_data_from_file(file_path)
|
|
|
|