Last active
January 2, 2016 16:09
-
-
Save johnjreiser/8328618 to your computer and use it in GitHub Desktop.
Generates a simple report showing matching schemas from a list of DBF files. Useful for seeing if a directory of shapefiles have similar attribute tables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Generates a simple report showing matching schemas from a list of DBF files. | |
""" | |
import sys, os, glob | |
from dbfpy import dbf | |
files = [] | |
for maybe_glob in sys.argv[1:]: | |
for filename in glob.iglob(maybe_glob): | |
if(os.path.exists(filename)): | |
files.append(filename) | |
fns = 0 | |
schemas = {} | |
for file in files: | |
db = dbf.Dbf(file) | |
fd = "\t".join(map(lambda x: str(x).split()[0], db.fieldDefs)) | |
if fns == 0: | |
fns = set(map(lambda x: str(x).split()[0], db.fieldDefs)) | |
else: | |
if not fns == None: | |
fns &= set(map(lambda x: str(x).split()[0], db.fieldDefs)) | |
if fd in schemas.keys(): | |
schemas[fd].append(file) | |
else: | |
schemas[fd] = [file] | |
# print schemas | |
for sd in schemas.keys(): | |
if(len(schemas[sd]) == 1): | |
print schemas[sd][0], "has the following unique schema:" | |
else: | |
print ", ".join(schemas[sd]), "share the following schema:" | |
print "\n".join(map(lambda x: " "+x, sd.split("\t"))) | |
if not fns == None: | |
print "These field names are shared amongst all:", ", ".join(fns) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment