编程语言
首页 > 编程语言> > Python 读取Shpfile 文件——Geopandas

Python 读取Shpfile 文件——Geopandas

作者:互联网

目标:根据条件筛选shpfile中所需要的文件,并导出。

# -*- coding: utf-8 -*-
"""
Created on Mon Sep 13 18:52:12 2021

"""

import pandas as pd
import geopandas
import shapefile
import datetime
import os

def ProcessShp(shpfilepath,GPStrajPath):
    #数据加载
    GPStrajdata=pd.read_csv(GPStrajPath)
    shpdata=geopandas.GeoDataFrame.from_file(shpfilepath)
    #使用道路提取
    GPStrajUsed=list(GPStrajdata["RoadID"])
    
    RoadIDuse=list(shpdata.RoadID.isin(GPStrajUsed))
    
    return RoadIDuse
if __name__ == "__main__":
    starttime = datetime.datetime.now()  
    
    path = "" #文件夹目录
  
 
    files= os.listdir(path) #得到文件夹下的所有文件名称
    i=1
    for file in files: #遍历文件夹
        if not os.path.isdir(file): #判断是否是文件夹,不是文件夹才打开
            if i==1:
                data=ProcessShp('D:/shp文件目录',path+"/"+file)
                usedata=data
                i=2
               
            else:
                usedata=usedata.append(data)
          
    
    shpdata=geopandas.GeoDataFrame.from_file('')
    #feature提取
    Shpuse=shpdata[shpdata.RoadID.isin(usedata)]
    #保存成shp
    Shpuse.to_file('name.shp',encoding = 'gb18030') 
    print ("耗时:"+"  "+str(datetime.datetime.now()-starttime))
    
    

标签:__,Shpfile,Python,Geopandas,datetime,文件夹,shpdata,file,import
来源: https://blog.csdn.net/u011914647/article/details/120282001