Example #1
0
def repair(featureClass):
    if not arcpy.Exists(featureClass):
        raise ExistsError   
    try:
        arcpy.RepairGeometry_management(featureClass)
    except:
        config.run_error_message(featureClass, "Repair geometry failure")
    gc.collect()
Example #2
0
def delete_feature_class(featureClass):
    try:
        gc.collect()
        if os.path.exists(featureClass):
            cmdDOS = "del " + featureClass[:(len(featureClass)-3)].replace("/", "\\") + "* /Q"
            os.system(cmdDOS)
        elif arcpy.Exists(featureClass):
            arcpy.Delete_management(featureClass)
    except:
        config.run_error_message(featureClass, "\n>>> FAILURE - can't delete " + featureClass + " <<<\n")
Example #3
0
 def explode(self, outputFeatureClass):    
     if not arcpy.Exists(self.catalogPath):
         raise ExistsError 
     if arcpy.Exists(outputFeatureClass):
         delete_feature_class(outputFeatureClass)
     try:            
         arcpy.MultipartToSinglepart_management(self.catalogPath, outputFeatureClass)
     except:
         config.run_error_message(self.catalogPath, "Exploding mulitparts failure")
     finally:
         gc.collect()
Example #4
0
def create_point_feature_class(pointFeatureClass):
    if arcpy.Exists(pointFeatureClass):
        delete_feature_class(pointFeatureClass)
        arcpy.RefreshCatalog(os.path.dirname(pointFeatureClass))
    try:
        arcpy.CreateFeatureclass_management(os.path.dirname(pointFeatureClass),
                                            os.path.basename(pointFeatureClass), "POINT")
        arcpy.RefreshCatalog(os.path.dirname(pointFeatureClass))
    except:
        config.run_error_message(pointFeatureClass, "Point feature class creation failure")
    gc.collect()
Example #5
0
def calculate_linker(featureClass):
    if not arcpy.Exists(featureClass):
        raise ExistsError
    try:
        arcpy.RefreshCatalog(os.path.dirname(featureClass))
        describeFeatureClass = arcpy.Describe(featureClass)
        oidField = "[" + describeFeatureClass.OIDFieldName + "]"
        arcpy.CalculateField_management(featureClass, "LINKER", oidField)
    except:
        config.run_error_message(featureClass, "LINKER calculation failure")
    gc.collect()
Example #6
0
def add_field_other(featureClass, fieldName, fieldType):
    if arcpy.TestSchemaLock(featureClass) == "False":
        raise SchemaLockError 
    try:
        fieldList = arcpy.ListFields(featureClass)
        for field in fieldList:
            if field.name == fieldName:
                delete_field(featureClass, fieldName)
        arcpy.AddField_management(featureClass, fieldName, fieldType, "", "",
                                  "", "", "NULLABLE", "NON_REQUIRED", "")
    except:
        config.run_error_message(featureClass, "Could not delete\add: " + fieldName)
Example #7
0
 def run_thread_counter(self):
     try:        
         finalCount = 0
         if len(self.threadList) == self.threadCount:
             timeElapsed = datetime.datetime.now() - self.timeList[0]
             for eachCount in self.threadList:
                 print(str(eachCount))
                 finalCount = finalCount + int(eachCount)                
             processingLabel = "\nFeatures Processed:\t" + str(finalCount) + "\nTime Elapsed:\t\t" + str(timeElapsed)
             print(processingLabel)
     except:
         config.run_error_message("Thread Count Failure")
Example #8
0
def explode(inputFeatureClass, outputFeatureClass):
    if not arcpy.Exists(inputFeatureClass):
        raise ExistsError 
    if arcpy.Exists(outputFeatureClass):
        delete_feature_class(outputFeatureClass)
    try:
        explodeFlag = "false"
        arcpy.MultipartToSinglepart_management(inputFeatureClass, outputFeatureClass)
        explodeFlag = "true"
    except:
        config.run_error_message(inputFeatureClass, "Exploding mulitparts failure")
    gc.collect()
    return explodeFlag
Example #9
0
 def run_thread_action(self):
     cnt = 0
     threadSpecific = threading.local()
     #threadSpecific.databaseCursor = dbinfo.getDatabaseConnection(database)
     threadFeature = self.layer.GetNextFeature()
     while threadFeature:
         cnt = cnt + 1
         if (cnt % 1000) == 0:
             timeElapsed = datetime.datetime.now() - self.timeList[0]
             print(cnt, str(timeElapsed))
         threadFeature = self.layer.GetNextFeature()
     try:
         self.threadList.append(cnt)
         self.run_thread_counter()
     except:
         config.run_error_message("Could not write final threading stats") 
Example #10
0
def integrate(featureClass):
    if not arcpy.Exists(featureClass):
        raise ExistsError     
    try:
        fcDesc = arcpy.Describe(featureClass)
        fcSR = fcDesc.SpatialReference
        linearUnit = string.upper(str(fcSR.LinearUnitName))
        if linearUnit in ("[M*0.30480060960121924]", "FOOT_US", "FOOT",
                          "[M*0.3048006096012192]", "M*0.3048006096012192", "FT"):
            arcpy.Integrate_management(featureClass, "0.8")
        elif linearUnit in ("METER", "M"):
            arcpy.Integrate_management(featureClass, "0.25")
        else:
            raise UnitError 
    except:
        config.run_error_message(featureClass, "Integration Failure")
    gc.collect()
Example #11
0
 def get_feature_wgs84_geometry(self, feature):
     try:
         geometry = feature.GetGeometryRef()
         cloneGeometry = geometry.Clone()
         sourceSR = osr.SpatialReference()
         sourceSR.ImportFromWkt(self.spatialReferenceWkt)
         wgs84 = osr.SpatialReference()
         wgs84.ImportFromProj4('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
         coordinateTransform = osr.CoordinateTransformation(sourceSR, wgs84)
         cloneGeometry.Transform(coordinateTransform)
         geometry = cloneGeometry
         #print str(cloneGeometry)
         #if str(self.spatialReferenceWkt).find("GCS_WGS_1984") == -1:
                         #geometry = self.get_feature_wgs84_geometry(feature)
                     #else:
                         #geometry = feature.GetGeometryRef()            
     except:               
         config.run_error_message("Geometry Transform Failure") 
     return geometry
Example #12
0
def polygon_cursor_loop(featureClass, pointFeatureClass):
    # This loop will add and calculate
    if not arcpy.Exists(featureClass):
        raise ExistsError 
    if not arcpy.Exists(pointFeatureClass):
        raise ExistsError
    try:
        polyDupeFlag = 1
        nextPolyDupeID,nextPolyTrueDupeID = 0, 0
        objNumber, MBRratio, MBRarea, Xlen, Ylen,  = 0, 0, 0, 0, 0
        polygonCursor, polygonRow, pointCursor, pointRow = None, None, None, None
        describeFeatureClass = arcpy.Describe(featureClass)
        shapeField = describeFeatureClass.shapeFieldName
        polyDupeDict = {}
        polyTrueDupeDict = {}
        pointCursor = arcpy.InsertCursor(pointFeatureClass)
        polygonCursor = arcpy.UpdateCursor(featureClass)
        polygonRow = next(polygonCursor)
        while polygonRow:
            geometry = polygonRow.getValue(shapeField)
            polyCentroid = str(geometry.centroid)
            polyTrueCentroid = str(geometry.trueCentroid)
            MBR = str(geometry.extent)
            if polyCentroid != polyTrueCentroid:
                polygonRow.setValue("FIP", "YES")
            pArea = geometry.area
            pointRow = pointCursor.newRow()
            Xcent = polyCentroid.split(" ")[0]
            Ycent = polyCentroid.split(" ")[1]
            Xmin = MBR.split(" ")[0]
            Ymin = MBR.split(" ")[1]
            Xmax = MBR.split(" ")[2]
            Ymax = MBR.split(" ")[3]
            Xlen = float(Xmax) - float(Xmin)
            Ylen = float(Ymax) - float(Ymin)
            MBRarea = Xlen * Ylen
            MBRratio = pArea/MBRarea 
            polygonRow.setValue("P_AREA", pArea)
            polygonRow.setValue("RATIO", MBRratio)
            objNumber = objNumber + 1
            geomPoint = arcpy.CreateObject("Point")
            geomPoint.X = str(Xcent)
            geomPoint.Y = str(Ycent)
            geomPoint.ID = str(objNumber)    
            pointRow.shape = geomPoint     
            pointRow.setValue("LINKER", polygonRow.getValue("LINKER"))
            #pointRow.ID = geomPoint.ID
            pointCursor.insertRow(pointRow)
            try:
                polyDupeID = polyDupeDict[polyCentroid]
                polygonRow.setValue("DUPE", polyDupeFlag)
            except:
                nextPolyDupeID = nextPolyDupeID + 1
                polyDupeID = nextPolyDupeID
                polyDupeDict[polyCentroid] = polyDupeID
            int_ring = 0
            partnum = 0
            partcount = geometry.partCount
            while partnum < partcount:
                part = geometry.getPart(partnum)
                pnt = next(part)
                pntcount = 0
                while pnt:
                    pnt = next(part)
                    pntcount += 1
                    if not pnt:
                        pnt = next(part)
                        if pnt:
                            int_ring = int_ring + 1
                partnum += 1
            if int_ring > 0:
                polygonRow.setValue("VOIDS", int_ring)
            polygonCursor.updateRow(polygonRow)
            polygonRow = next(polygonCursor)           
    except:
        config.run_error_message(featureClass, "Polygon cursor failure")
    finally:
        del polygonCursor, polygonRow, pointCursor, pointRow
    gc.collect()
Example #13
0
def delete_field(featureClass, field):
    if arcpy.Exists(featureClass):
        try:
            arcpy.DeleteField_management(featureClass, field)
        except:
            config.run_error_message(featureClass, "Delete field failure")
Example #14
0
            print "shape type:", myFcObject.shapeType
            print "data type:", myFcObject.dataType
            print "base name:", myFcObject.baseName
            print "feature count", myFcObject.get_count()
            print "has z:", myFcObject.hasZ
            print "has m:", myFcObject.hasM
            print "spatial reference name:", myFcObject.spatialReferenceName
            print "spatial reference type:", myFcObject.spatialReferenceType
            print "spatial reference abbr name:", myFcObject.spatialReferenceAbbrName
            print "spatial reference PCS name:", myFcObject.spatialReferencePcsName
            print "spatial reference linear unit:", myFcObject.spatialReferenceLinearUnitName
            print "\n"
            myFcObject.test_schema_lock()
            if myFcObject.dataType == "ShapeFile":
                outputFcPath = myFcObject.path + os.sep + "temp.shp"
            else:
                outputFcPath = myFcObject.path + os.sep + "temp"
            myFcObject.explode(outputFcPath)
            myOutFcObject = esriFeatureClass(outputFcPath)
            print "feature count", myOutFcObject.get_count()
            myOutFcObject.add_spatial_index()
            myOutFcObject.compact()
            myOutFcObject.add_field_string("CHUCK1", "35")
            myOutFcObject.add_field_other("cr_long1", "LONG")
            myOutFcObject.repair()
            # myOutFcObject.integrate()
    except:
        config.run_error_message("Class Fail")
    finally:
        gc.collect()
Example #15
0
 def repair_shapefile(self):
     try:
         shpDriver = ogr.GetDriverByName('ESRI Shapefile')
         print("Drivers are ready")
         shpdata = self.shapefilePath
         output = os.path.dirname(shpdata) + os.sep + "rep_" + os.path.basename(shpdata)[4:]
         if os.path.exists(output):
             shpDriver.DeleteDataSource(output)
         ds = shpDriver.CreateDataSource(output)    
         shpSource = shpDriver.Open(shpdata, 0)
         if shpSource is None:
             print('Could not open ' + shpdata)
             sys.exit(1) #exit with an error code
         layer = shpSource.GetLayer()
         numFeatures = layer.GetFeatureCount()
         print('Feature count:' + str(numFeatures))
         inputSR = layer.GetSpatialRef()      
         shplayer = ds.CreateLayer(output, inputSR, ogr.wkbPolygon)       
         inFeature = layer.GetNextFeature()
         for index in range(0, inFeature.GetFieldCount()):
             #fieldList.append(inFeature.GetFieldDefnRef(index))
             shplayer.CreateField(inFeature.GetFieldDefnRef(index))
             print('Created field: ' + inFeature.GetFieldDefnRef(index).name)      
         cnt = 0
         while inFeature:
             cnt = cnt + 1
             f = ogr.Feature(shplayer.GetLayerDefn())
             for index in range(0, f.GetFieldCount()):
                 try:
                     attValue = inFeature.GetField(inFeature.GetFieldDefnRef(index).name)
                     if attValue:
                         f.SetField(f.GetFieldDefnRef(index).name, inFeature.GetField(inFeature.GetFieldDefnRef(index).name))
                 except:
                     print(inFeature.GetFieldDefnRef(index).name + ' - skipping attribute value adding null')
             try:
                 geom = loads(inFeature.GetGeometryRef().ExportToWkb())
                 if geom.is_valid:
                     newGeom = inFeature.geometry()     
                 else:
                     print("Bad geom: " + str(inFeature.GetFID()))
                     print(explain_validity(geom))
                     cleanGeom = geom.buffer(0.0)
                     if cleanGeom.is_valid:
                         print("Geometry is clean")
                         wkbGeom = dumps(cleanGeom)
                         newGeom = ogr.CreateGeometryFromWkb(wkbGeom)
                     else:
                         newGeom = inFeature.geometry()
                 f.SetGeometry(newGeom)
                 if shplayer.CreateFeature(f) != 0:
                     print("Failed to create feature in shapefile.\n")
                     sys.exit(1)
                 f.Destroy()
             except:
                 print('Skipping null geometry')
                 config.run_error_message('repair geom feature failure null geom..')
                 break
             inFeature = layer.GetNextFeature()
         print('Feature count from loop:' + str(cnt))
         ds.Destroy() 
     except:
         config.run_error_message("Repair failure")        
Example #16
0
                        if cleanGeom.is_valid:
                            print("Geometry is clean")
                            wkbGeom = dumps(cleanGeom)
                            newGeom = ogr.CreateGeometryFromWkb(wkbGeom)
                        else:
                            newGeom = inFeature.geometry()
                    f.SetGeometry(newGeom)
                    if shplayer.CreateFeature(f) != 0:
                        print("Failed to create feature in shapefile.\n")
                        sys.exit(1)
                    f.Destroy()
                except:
                    print('Skipping null geometry')
                    config.run_error_message('repair geom feature failure null geom..')
                    break
                inFeature = layer.GetNextFeature()
            print('Feature count from loop:' + str(cnt))
            ds.Destroy() 
        except:
            config.run_error_message("Repair failure")        

if __name__ == '__main__':
    try:
        print("Running shpinfo...")
        myShp = OgrShapefile('C:\\work\\data\\county\\County_utf8.shp')
        myShp.repair_shapefile()
        print("Finished")
    except:
        config.run_error_message("shpinfo connection failure")