Merge Coverage files to a single shapefile

573
1
07-29-2012 07:11 PM
GarySham
New Contributor
Dear All,

I am a beginner of writing Python in ArcMap. I would like to write a Python script to merge coverage files to a single shape file and add this script as a new tool in the Arc Toolbox.

Do you have any tips and suggestion for me ?

Thank you for consideration and help.

Regards
Gary
Tags (2)
0 Kudos
1 Reply
KimOllivier
Occasional Contributor III
I assume you are loading a library into a file geodatabase. Forget shapefiles, so last century. But I used to do this, by making up a folder for each tile and have shapefiles there. I now append all the coverages together into a single featureclass for each layer.

Here is some old code with the main function included. Key features to note are:
A dictionary to rename the coverages to shapefiles, otherwise they are all point, line, polygon.
I used a complicated dropfields to remove redundant system fields to get around a bug at the time.
You have to be in the workspace to list coverages which appear a featuredatasets to Python.
A list is much more flexible to re-run after crashes to redo the load with a smaller list.
I accessed the tiles in the library directly.
Field names were automatically truncated which may be a problem, you could now use fieldmapping, but not if you are a beginner.
Some functions removed to meet posting linecount limit.
# MobileDirectLoad.py
# load coverage tiles
# Kimo Nov 2006
# With simple toolbox
# or just run from PythonWin
#
# load directly from coverages 12 April 2012
# except for parcels which are multipart direct load
#   from currentgdb/parcel.gdb

# ArcGIS 9.1
# ArcGIS 9.3+ October  2009
# bugfix 21 Oct 2009 in warning message
# 10.0 with 9.3 gdb format 10 Nov 2011
# 10.0 upgrade
# 14 April 2012
# 19 June 2012 bug fix for ccaddress
import arcpy,sys,os,datetime

# -- functions removed --
#======================== main =============================
start = datetime.datetime.now()
droplst = ["xxx","rings_ok","rings_nok","lpoly#","rpoly#",
"fnode#","tnode#","length","area","perimeter",
"lpoly","rpoly","parcel#","parcel-id","poly#",
"sufi","toc-code","parcel-int",
"total-area","addressp", "pre-dir", "street-name",
"street-type", "suf-dir","name-crs","alt-id",
"$polygonid",
"range_low","range_high","unofficial",
"multi","case1","case2","case3",
"$SCALE","$ANGLE","PAR-ID","OTHR#","OTHR-ID",]

sr = arcpy.SpatialReference()
sr.factoryCode = 2193 # NZTM
sr.create()
arcpy.env.outputCoordinateSystem = sr

try:
        gdb = sys.argv[1]  # "e:/crs/mobile/mobile.gdb"
        repl = sys.argv[2]
        arcpy.AddMessage(repl)
        if repl.lower() == 'true' :
                replace = True
        elif not repl.lower() == 'false':
                replace = False
except :
        gdb = "e:/crs/mobile/mobile.gdb"
        replace =False

ws = "e:/lib/nztm/tile"
        
if not arcpy.Exists(gdb) :
        dir = os.path.dirname(gdb)
        filegdb = os.path.basename(gdb)
        print dir,filegdb
        arcpy.management.CreateFileGDB(dir,filegdb,"9.3")
arcpy.env.workspace = ws+"/t1001/data"

dictCov = {    "add_all":"address/point",      
               "emf":"emf/point",
               "fename":"fename/point",
               "hydro":"hydro/polygon",
               "hydrobdy":"hydro/arc",
               "lparcel":"lparcel/arc",
               "olabel":"olabel/point",
               "owner":"owner/point",
               "parcel":"parcel/polygon",
               "parcelbdy":"parcel/arc",
               "perror":"perror/polygon",
               "plabel":"plabel/point",
               "plan":"plan/point",
               "pother":"pother/region.othr",
               "strata":"pother/region.othr",
               "rail":"rail/arc",
               "res":"res/polygon",
               "reserve":"res/polygon",
               "road":"road/arc",
               "shway":"shway/arc",
               "surveypt":"surveypt/point",
               "title":"title/point",
               "titledif":"titledif/point",
               "up":"up/point",
               "uparcel":"uparcel/point"
           }
lstFC = ["add_all","title","fename","road","rail","strata","plabel","olabel","lparcel","up","uparcel","surveypt","plan","perror","reserve","parcelbdy",]
# lstFC = ["title","fename","parcelbdy","road","rail","strata","plabel","olabel","lparcel","up","uparcel","surveypt","plan","perror","reserve"]
# parcel removed because multipart from elsewhere
# lstFC = ["add_all"]
# xlabel removed 13 July 2007
# address renamed because special processing required
for outFCName in lstFC :
    cov = dictCov[outFCName]
    fds = cov.split("/")[0]
    print "Processing",outFCName,cov
    arcpy.AddMessage(outFCName) 
    if not arcpy.Exists(gdb+"/"+outFCName) :
            lstSrc = []
            # merge the coverages in all the tiles from 1001 - 1012 
            for ld in range(12) :
                tile = "t%d" % (ld + 1001)
                srcCov = ws+"/"+tile+"/data/"+cov
                if arcpy.Exists(srcCov) :
                    lstSrc.append(srcCov)
                else :
                    arcpy.AddError(srcCov+'not found')
                    print srcCov,"NOT FOUND"
            # Create FieldMappings object to manage merge output fields
            fieldMappings = arcpy.CreateObject("FieldMappings")
            # Add all fields from sources
            for src in lstSrc:
                fieldMappings.addTable(src)
            # hide fields
            print "    removing ",
            for fld in [fds+"#",fds+"-ID"] + droplst: #include cov# and cov-id
                pos = fieldMappings.findFieldMapIndex(fld.upper())
                # print pos,fld
                if pos >= 0 :
                    fieldMappings.removeFieldMap(pos)
                    print fld.upper(),
            print
            print "    keeping  ",
            for x in range(fieldMappings.fieldCount):
                print fieldMappings.getFieldMap(x).outputField.name,
            print
            arcpy.Merge_management(lstSrc, gdb+"/"+outFCName,fieldMappings )
            print outFCName,"merged"
            arcpy.AddMessage(outFCName+" merged")
    else :
            print "Skipping load of",outFCName
            arcpy.AddWarning("Skipping load of "+outFCName)

print "Done", datetime.datetime.now() - start
arcpy.AddMessage("Done")
0 Kudos