Search code examples
pythonsearchgisarcpy

searching for shapefile within a directory


I have a file directory that has surge data by year and month and the administrative units they have impacted (e.g. surge in july 1977 and a list of communes it has impacted). I also have a directory of precipitation data also by month and year. I need to perform one GIS operation called Near and then make a tabular join of precipitation data and surge data so that the surge month/year would match with precip month/year.

Generally, the pseudo-code for the process I'm describing is the following: for (surges from 1977 to 2006) { if(surge==1977 july) { find precipitation 1977 july from a directory run Near on the file that was found make a tabular join of surge 1977 July and precip 1977 July by NearFID } }

How to do this process in python and how to search for a necessary file in the directory?


Solution

  • Give the the provided information, we'll need to make some assumptions:

    1. All precip shapefiles are in one directory.
    2. precip files have some random element in their filename, thus making it necessary to search inside of refer directly to the shape file by name.
    3. precip files have identifiable month and year in filename.

    Given these assumptions we can use Python's glob module to find the shape files for a particular month and year.

    Given surge files in directory (/path/to/surge/):

    surge_july_2000.shp, surge_august_2000.shp,etc.
    

    and precip files in directory (/path/to/precip):

    precip_random123_july_2000.shp, precip_random8482_august_2000.shp, etc
    

    We can modify your pseudo-code to look something like this:

    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])
    

    It would be difficult to work around assumption #3, the others however are trivial.