Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在目录中搜索shapefile_Python_Search_Gis_Arcpy - Fatal编程技术网

Python 在目录中搜索shapefile

Python 在目录中搜索shapefile,python,search,gis,arcpy,Python,Search,Gis,Arcpy,我有一个文件目录,其中按年份和月份列出了激增数据,以及受影响的行政单位(例如,1977年7月的激增及其影响的公社列表)。我还有一个降水量数据目录,也按月份和年份分类。我需要执行一个名为Near的GIS操作,然后将降水数据和涌浪数据进行表格连接,以便涌浪月/年与precip月/年匹配 通常,我所描述的流程的伪代码如下所示: 年(1977年至2006年激增){ 如果(激增=1977年7月){ 从目录中查找数据 在找到的文件附近运行 通过NearFID将1977年7月的喘振与1977年7月的preci

我有一个文件目录,其中按年份和月份列出了激增数据,以及受影响的行政单位(例如,1977年7月的激增及其影响的公社列表)。我还有一个降水量数据目录,也按月份和年份分类。我需要执行一个名为Near的GIS操作,然后将降水数据和涌浪数据进行表格连接,以便涌浪月/年与precip月/年匹配

通常,我所描述的流程的伪代码如下所示: 年(1977年至2006年激增){ 如果(激增=1977年7月){ 从目录中查找数据 在找到的文件附近运行 通过NearFID将1977年7月的喘振与1977年7月的precip进行表格连接 } }


如何在python中执行此过程,以及如何在目录中搜索必要的文件?

给出提供的信息,我们需要做出一些假设:

  • 所有precip形状文件都在一个目录中
  • precip文件的文件名中有一些随机元素,因此有必要在文件中搜索,直接按名称引用形状文件
  • precip文件在文件名中具有可识别的月份和年份
  • 根据这些假设,我们可以使用Python的glob模块来查找特定月份和年份的形状文件

    目录(/path/to/surge/)中给定的喘振文件:

    和目录(/path/to/precip)中的precip文件:

    我们可以修改您的伪代码,使其看起来像这样:

    import glob
    for curSurge in glob.glob("/path/to/surges/*.shp"):
        blah, month, year = curSurge.split('.')[0].split('_')
        matches = glob.glob('/path/to/precip/*_%s_%s.shp'%(month, year))
        if len(matches) != 1:
            raise Exception, "Oh No!, We found %d matches instead of 1!"%(len(matches))
        run_near_and_make_tabular_join(curSurge, matches[0])
    

    很难绕过假设#3,但其他假设都很琐碎。

    您如何知道数据文件的月份和年份?它是文件名的一部分吗?@wwii是的,我将它们命名为国家/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区/地区。
    precip_random123_july_2000.shp, precip_random8482_august_2000.shp, etc
    
    import glob
    for curSurge in glob.glob("/path/to/surges/*.shp"):
        blah, month, year = curSurge.split('.')[0].split('_')
        matches = glob.glob('/path/to/precip/*_%s_%s.shp'%(month, year))
        if len(matches) != 1:
            raise Exception, "Oh No!, We found %d matches instead of 1!"%(len(matches))
        run_near_and_make_tabular_join(curSurge, matches[0])