分享名称:gpdCgdb.zip
分享链接:http://www.giseden.xyz:12080/#s/9AQYO4cA
访问密码:eKc12
import pandas as pd import geopandas as gpd import wx import os import fiona from shapely.geometry import shape import json fiona.supported_drivers['KML'] = 'rw' fiona.supported_drivers['OpenFileGDB'] = 'rw' # 读取文件路径 def get_path(wildcard, msg): style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST dialog = wx.FileDialog(None, msg, wildcard=wildcard, style=style) if dialog.ShowModal() == wx.ID_OK: path = dialog.GetPath() else: path = None dialog.Destroy() return path def get_dir(msg): dir = '' dlg = wx.DirDialog(None, msg, style=wx.DD_DEFAULT_STYLE) if dlg.ShowModal() == wx.ID_OK: dir = dlg.GetPath() # 文件夹路径 return dir def read_shp(shp_path): # Open the shapefile using fiona crs = '' with fiona.open(shp_path, 'r',encoding='gbk') as source: crs = source.crs['init'] # Filter out invalid features filtered_features = [] for feature in source: try: if len(feature['geometry']['coordinates'])>1: if feature['geometry'] is not None and gpd.GeoSeries([shape(feature['geometry'])]).is_valid.iloc[0]: filtered_features.append(feature) except Exception: print(1) # Convert the filtered features to a GeoDataFrame return gpd.GeoDataFrame.from_features(filtered_features).set_crs(crs) class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, title="shp转kml", size=(400, 300)) self.panel = wx.Panel(self) self.botton_segments = wx.Button(self.panel, label="shp转换kml") # 定义按钮 self.textctrl = wx.TextCtrl( self.panel, -1, size=(300, 100), style=wx.TE_MULTILINE) # 定义富文本框 self.textctrl.SetValue('{"自来水线": "JPLN","自来水点": "JPPT","路灯线":"LDLN","路灯点":"LDPT"}') # 建立三个水平布局和一个垂直布局 self.boxsizer_h1 = wx.BoxSizer(wx.HORIZONTAL) # 创建水平布局 self.boxsizer_h1.Add(self.botton_segments, proportion=1, border=8, flag=wx.LEFT) # 添加按钮,并设置按钮大小和左右间距 self.boxsizer_h2 = wx.BoxSizer(wx.HORIZONTAL) # 创建水平布局 self.boxsizer_h2.Add(self.textctrl, proportion=1, border=8, flag=wx.LEFT | wx.RIGHT) self.boxsizer_v = wx.BoxSizer(wx.VERTICAL) self.boxsizer_v.Add( self.boxsizer_h1, flag=wx.EXPAND | wx.TOP, border=8) self.boxsizer_v.Add( self.boxsizer_h2, flag=wx.EXPAND | wx.TOP, border=8) self.panel.SetSizer(self.boxsizer_v) self.botton_segments.Bind( wx.EVT_BUTTON, self.calculate_segments) # 绑定点击函数 self.Show(True) # 消息提醒成果 def dialoginfo(self, msg, title): toastone = wx.MessageDialog( None, msg, title, wx.YES_DEFAULT | wx.ICON_QUESTION) if toastone.ShowModal() == wx.ID_YES: # 如果点击了提示框的确定按钮 toastone.Destroy() # 则关闭提示框 def calculate_segments(self,event): shp_dir = get_dir("请选取shp文件文件夹") # gdb_file = os.path.join(shp_dir,'输出.gpkg') gdb_file = os.path.join(shp_dir,'输出.gdb') excel_file = get_path("EXCEL file(*.xlsx;*.xls)|*.xlsx;*.xls", '请输入excel文件的路径') excel_data = pd.ExcelFile(excel_file) sheet_names = excel_data.sheet_names layer_dict = json.loads(self.textctrl.GetValue()) for sheet_name in sheet_names: shp_file = os.path.join(shp_dir,sheet_name+".shp") if os.path.exists(shp_file): sheet = pd.read_excel(excel_file,sheet_name=sheet_name) gpf = read_shp(shp_file) new_columns = {} drop_columns = [] for index, row in sheet.iterrows(): if not pd.isnull(row['GDB']) and pd.isnull(row['SHP']): gpf[row['GDB']] = None elif not pd.isnull(row['GDB']) and not pd.isnull(row['SHP']): new_columns[row['SHP']] = row['GDB'] elif pd.isnull(row['GDB']) and not pd.isnull(row['SHP']): drop_columns.append(row['SHP']) else: pass gpf = gpf.drop(columns=drop_columns) gpf = gpf.rename(columns=new_columns) gpf.to_file(gdb_file,driver="FileGDB",layer=layer_dict[sheet_name],encoding='utf-8') # gpf.to_file(gdb_file,driver="GPKG",layer=layer_dict[sheet_name],encoding='utf-8') # gpf.to_file(os.path.join(shp_dir,layer_dict[sheet_name]+".kml"),driver="KML",encoding='utf-8') self.dialoginfo("生成成功","消息") if __name__=="__main__": app = wx.App() window = MyFrame() app.MainLoop()