-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_utils.py
349 lines (284 loc) · 13.1 KB
/
file_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import ezdxf
import geopandas as gpd
import json
import os
import osmnx as ox
import pandas as pd
import sqlite3
import tkinter as tk
from requests import Request
from shapely.geometry import Point, LineString
from sqlalchemy import create_engine
import tiledb
from tkinter import Tk, filedialog
# create a global variable to store the mapping between aliases and file paths
file_path_mapping = {}
def import_data():
root = Tk()
root.withdraw()
file_path = filedialog.askopenfilename(filetypes=[("All supported files", "*.*"), ("Shapefile", "*.shp"), ("GeoJSON", "*.geojson"), ("CSV", "*.csv"), ("GDB", "*.gdb"), ("DXF", "*.dxf"), ("GML", "*.gml"), ("GPKG", "*.gpkg"), ("KML", "*.kml"), ("OSM", "*.osm"), ("SQLite", "*.sqlite"), ("TileDB", "*.tdb"), ("Excel files", "*.xls;*.xlsx")])
return file_path
def read_file(file_path, x_col=None, y_col=None, crs=None):
if file_path.endswith(".shp"):
data = gpd.read_file(file_path)
data = data.to_crs(epsg=4326)
data.to_file(file_path[:-4] + ".geojson", driver="GeoJSON")
return data
elif file_path.endswith(".geojson") or file_path.endswith(".json") or file_path.endswith(".topojson"):
data = gpd.read_file(file_path)
data = data.to_crs(epsg=4326)
return data
elif file_path.endswith(".csv"):
data = gpd.read_file(file_path)
return data
elif file_path.endswith(".gdb"):
layers = fiona.listlayers(file_path)
data = gpd.read_file(file_path, layer=layers[0])
return data
elif file_path.endswith(".dxf"):
# Read the DXF file
doc = ezdxf.readfile(file_path)
msp = doc.modelspace()
# Extract points and lines from the DXF file
points = []
lines = []
for entity in msp:
if entity.dxftype() == 'POINT':
points.append(Point(entity.dxf.location))
elif entity.dxftype() == 'LINE':
start = entity.dxf.start
end = entity.dxf.end
lines.append(LineString([start, end]))
# Create a GeoDataFrame from the extracted data
data = gpd.GeoDataFrame(geometry=points + lines)
return data
elif file_path.endswith(".gml"):
data = gpd.read_file(file_path)
return data
elif file_path.endswith(".gpkg"):
layers = fiona.listlayers(file_path)
data = gpd.read_file(file_path, layer=layers[0])
return data
elif file_path.endswith(".kml"):
data = gpd.read_file(file_path, driver='KML')
return data
elif file_path.endswith(".osm"):
# Read the OSM file
graph = ox.graph_from_xml(file_path)
nodes, edges = ox.graph_to_gdfs(graph)
# Create a GeoDataFrame from the extracted data
data = gpd.GeoDataFrame(geometry=nodes.geometry)
return data
elif file_path.endswith(".sqlite"):
# Connect to the SQLite database
conn = sqlite3.connect(file_path)
cursor = conn.cursor()
# Execute a query to get the data from the database
cursor.execute("SELECT * FROM my_table")
data = cursor.fetchall()
# Create a DataFrame from the extracted data
df = pd.DataFrame(data, columns=[col[0] for col in cursor.description])
# Convert the DataFrame into a GeoDataFrame
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.longitude, df.latitude))
return gdf
elif file_path.endswith(".tdb"):
# Open the TileDB array for reading
with tiledb.open(file_path, mode='r') as arr:
# Query the data from the array
data = arr[:]
# Create a DataFrame from the extracted data
df = pd.DataFrame(data)
# Convert the DataFrame into a GeoDataFrame
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.longitude, df.latitude))
return gdf
elif file_path.endswith(".xls") or file_path.endswith(".xlsx"):
# Read the Excel file
df = pd.read_excel(file_path)
# Convert the DataFrame into a GeoDataFrame
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df[x_col], df[y_col]), crs=crs)
return gdf
def open_file_dialog():
root = Tk()
root.withdraw()
file_paths = filedialog.askopenfilenames(filetypes=[("All supported files", "*.*"), ("Shapefiles", "*.shp"), ("GeoJSON files", "*.geojson"), ("CSV files", "*.csv"), ("GDB files", "*.gdb"), ("DXF files", "*.dxf"), ("GML files", "*.gml"), ("GPKG files", "*.gpkg"), ("KML files", "*.kml"), ("OSM files", "*.osm"), ("SQLite files", "*.sqlite"), ("TileDB files", "*.tdb"), ("Excel files", "*.xls;*.xlsx")])
result = []
for i, file_path in enumerate(file_paths):
if file_path.endswith(".shp"):
gdf = gpd.read_file(file_path)
geojson_path = file_path.replace(".shp", ".geojson")
gdf.to_file(geojson_path, driver="GeoJSON")
file_path = geojson_path
elif file_path.endswith(".xls") or file_path.endswith(".xlsx"):
# Read the Excel file
df = pd.read_excel(file_path)
# Convert the DataFrame into a GeoDataFrame
# You will need to specify the columns in the DataFrame that contain the x and y coordinates of the geometry data
x_col = 'longitude'
y_col = 'latitude'
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df[x_col], df[y_col]))
# Save the GeoDataFrame as a GeoJSON file
geojson_path = file_path.replace(".xls", ".geojson").replace(".xlsx", ".geojson")
gdf.to_file(geojson_path, driver="GeoJSON")
file_path = geojson_path
gdf = gpd.read_file(file_path)
gdf = gdf.to_crs("EPSG:4326")
gdf.to_file(file_path, driver="GeoJSON")
result.append(file_path)
return result
def add_files(listbox):
file_paths = open_file_dialog()
for file_path in file_paths:
# Use os.path.basename to get the filename without the path
# Use os.path.splitext to split the filename and extension
name, _ = os.path.splitext(os.path.basename(file_path))
# store the mapping between the alias and the full file path
file_path_mapping[name] = file_path
listbox.insert(tk.END, name)
def on_select(listbox, property_listbox):
selected_items = listbox.curselection()
for item in selected_items:
value = listbox.get(item)
# look up the full file path based on the alias
file_path = file_path_mapping[value]
load_vector_data(file_path, property_listbox)
def load_vector_data(file_path, property_listbox):
with open(file_path, "r") as f:
geojson_data = json.load(f)
property_names = set()
for feature in geojson_data["features"]:
for property_name, property_value in feature["properties"].items():
# Check if the property value is a valid data type
if isinstance(property_value, (str, int, float, bool, list, dict)):
property_names.add(property_name)
# Sort the property names in alphabetical order
property_names = sorted(property_names)
property_listbox.delete(0, tk.END)
for property_name in property_names:
property_listbox.insert(tk.END, property_name)
def read_dxf(file_path):
# Read the DXF file
doc = ezdxf.readfile(file_path)
msp = doc.modelspace()
# Extract points and lines from the DXF file
points = []
lines = []
for entity in msp:
if entity.dxftype() == 'POINT':
points.append(Point(entity.dxf.location))
elif entity.dxftype() == 'LINE':
start = entity.dxf.start
end = entity.dxf.end
lines.append(LineString([start, end]))
# Create a GeoDataFrame from the extracted data
data = gpd.GeoDataFrame(geometry=points + lines)
return data
def read_osm(file_path):
# Read the OSM file
graph = ox.graph_from_xml(file_path)
nodes, edges = ox.graph_to_gdfs(graph)
# Create a GeoDataFrame from the extracted data
data = gpd.GeoDataFrame(geometry=nodes.geometry)
return data
def read_sqlite(file_path):
# Connect to the SQLite database
conn = sqlite3.connect(file_path)
cursor = conn.cursor()
# Execute a query to get the data from the database
cursor.execute("SELECT * FROM my_table")
data = cursor.fetchall()
# Create a DataFrame from the extracted data
df = pd.DataFrame(data, columns=[col[0] for col in cursor.description])
# Convert the DataFrame into a GeoDataFrame
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.longitude, df.latitude))
return gdf
def read_postgis_dump(dump_file, table_name, geom_col, crs=None):
# Restore the dump into a PostgreSQL database
# This step assumes that you have already created a PostgreSQL database
# and have the necessary permissions to restore data into it
db_name = "my_database"
db_user = "my_user"
db_password = "my_password"
db_host = "localhost"
db_port = 5432
restore_command = f"pg_restore -d {db_name} -U {db_user} -h {db_host} -p {db_port} {dump_file}"
os.system(restore_command)
# Create a SQLAlchemy engine to connect to the database
engine = create_engine(f"postgresql://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}")
# Read data from the specified table into a GeoDataFrame
sql = f"SELECT * FROM {table_name}"
gdf = gpd.read_postgis(sql, engine, geom_col=geom_col, crs=crs)
return gdf
def read_tiledb(uri):
# Open the TileDB array for reading
with tiledb.open(uri, mode='r') as arr:
# Query the data from the array
data = arr[:]
# Create a DataFrame from the extracted data
df = pd.DataFrame(data)
# Convert the DataFrame into a GeoDataFrame
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.longitude, df.latitude))
return gdf
def read_topojson(file_path):
# Read the TopoJSON file
data = gpd.read_file(file_path)
return data
def read_wfs(wfs_url, layer_name):
# Specify the parameters for fetching the data
params = dict(service='WFS', version="2.0.0", request='GetFeature', typeName=layer_name, outputFormat='json')
# Parse the URL with parameters
wfs_request_url = Request('GET', wfs_url, params=params).prepare().url
# Read data from URL
data = gpd.read_file(wfs_request_url)
return data
def read_excel(file_path, x_col, y_col, crs=None):
# Read the Excel file
df = pd.read_excel(file_path)
# Convert the DataFrame into a GeoDataFrame
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df[x_col], df[y_col]), crs=crs)
return gdf
def set_working_object_a(listbox, text_widget_a):
selected_file_indices = listbox.curselection()
if not selected_file_indices:
message = "Please select a file"
print(message)
# delete the existing content of the text widget
text_widget_a.delete("1.0", tk.END)
# insert the new message into the text widget
text_widget_a.insert(tk.END, message + "\n")
return None
selected_file_index = selected_file_indices[0]
selected_file_alias = listbox.get(selected_file_index)
# look up the full file path based on the alias
selected_file = file_path_mapping[selected_file_alias]
# Load the geojson data into a geopandas GeoDataFrame
working_object_a = gpd.read_file(selected_file)
# set the source_file attribute of the GeoDataFrame
working_object_a.source_file = selected_file
message = f"Working object geojson data set to {selected_file}"
print(message)
# delete the existing content of the text widget
text_widget_a.delete("1.0", tk.END)
# insert the new message into the text widget
text_widget_a.insert(tk.END, message + "\n")
return working_object_a
def set_working_object_b(property_listbox, text_widget_b):
selected_property_indices = property_listbox.curselection()
if not selected_property_indices:
message = "Please select a property"
print(message)
# delete the existing content of the text widget
text_widget_b.delete("1.0", tk.END)
# insert the new message into the text widget
text_widget_b.insert(tk.END, message + "\n")
return None
selected_property_index = selected_property_indices[0]
selected_property = property_listbox.get(selected_property_index)
# Set the working object b as the selected property
working_object_b = selected_property
message = f"Property of working object set to {selected_property}"
# delete the existing content of the text widget
text_widget_b.delete("1.0", tk.END)
# insert the new message into the text widget
text_widget_b.insert(tk.END, message + "\n")
print(f"working_object_b: {working_object_b}")
return working_object_b