WPDx_Toolset.pyt
def execute(self, parameters, messages):
        """Calculates rural population in each administrative area."""

        admin = join(dirname(__file__), "Data", "ToolData.gdb", "Admin")

        #set up a scratch workspace and set it as env
        scratch = tempfile.mkdtemp()
        gdb = arcpy.CreateFileGDB_management(scratch, "temp").getOutput(0)
        #arcpy.env.scratchGDB = gdb
        #arcpy.env.workspace = gdb
        #arcpy.env.scratchWorkspace = gdb
        country = parameters[0].valueAsText

        if len(country) > 2:
            query_type = 'Country'
        else:
            query_type = 'cc'
        mask = setEnvironment(country, query_type)


        # arcpy.env.snapRaster = pop_grid
        cell_size = '0.0008333'
        start = time.clock()
        area_served = arcpy.PolygonToRaster_conversion(join(dirname(__file__), "Data", "ToolData.gdb", "Urban"),
                                                       'RANK',
                                                       join(gdb, 'served'),
                                                       'CELL_CENTER', 'NONE',
                                                       cell_size)


        area_not_served = arcpy.gp.IsNull  (area_served)
        arcpy.AddMessage("Rasterize took: {:.2f} seconds".format(time.clock() - start))
        start = time.clock()

        # Get path to population data
        with open(join(dirname(__file__), "Data", "Paths.txt")) as paths_ref:
            for line in paths_ref:
                #try:
                parts = line.split('...')
                name = parts[0]
                pop_grid = parts[1]

                pop_not_served = arcpy.sa.Con(area_not_served, pop_grid, '0', 'Value>0')
                arcpy.AddMessage("Con took: {:.2f} seconds".format(time.clock() - start))

                start = time.clock()
                pop_by_region = arcpy.gp.ZonalStatisticsAsTable_sa(admin,
                                                           'Name',
                                                           pop_not_served,
                                                           r"in_memory\pop{}".format(name),
                                                           '', 'SUM')
                arcpy.AddMessage("Zonal Stats took: {:.2f} seconds".format(time.clock() - start))
                pop_dict = dict()
                with arcpy.da.SearchCursor(pop_by_region, ['Name', 'SUM']) as cursor:
                    for row in cursor:
                        pop_dict[row[0]] = row[1]

                with arcpy.da.UpdateCursor(admin, ['Name', 'Rural_Pop_{}'.format(name)],
                                                  "{} = '{}'".format(query_type, country)
                                                  ) as cursor:
                    for row in cursor:
                        try:
                            row[1] = pop_dict[row[0]]
                            cursor.updateRow(row)
                        except KeyError:
                            pass
                #except:
                 #   arcpy.AddError("ERROR: No {} data available in this region".format(name))
Calculates rural population in each administrative area.
Calculates rural population in each administrative area.
Calculates rural population in each administrative area .
def execute(self, parameters, messages):
        admin = join(dirname(__file__), "Data", "ToolData.gdb", "Admin")
        scratch = tempfile.mkdtemp()
        gdb = arcpy.CreateFileGDB_management(scratch, "temp").getOutput(0)
        country = parameters[0].valueAsText
        if len(country) > 2:
            query_type = 'Country'
        else:
            query_type = 'cc'
        mask = setEnvironment(country, query_type)
        cell_size = '0.0008333'
        start = time.clock()
        area_served = arcpy.PolygonToRaster_conversion(join(dirname(__file__), "Data", "ToolData.gdb", "Urban"),
                                                       'RANK',
                                                       join(gdb, 'served'),
                                                       'CELL_CENTER', 'NONE',
                                                       cell_size)
        area_not_served = arcpy.gp.IsNull  (area_served)
        arcpy.AddMessage("Rasterize took: {:.2f} seconds".format(time.clock() - start))
        start = time.clock()
        with open(join(dirname(__file__), "Data", "Paths.txt")) as paths_ref:
            for line in paths_ref:
                parts = line.split('...')
                name = parts[0]
                pop_grid = parts[1]
                pop_not_served = arcpy.sa.Con(area_not_served, pop_grid, '0', 'Value>0')
                arcpy.AddMessage("Con took: {:.2f} seconds".format(time.clock() - start))
                start = time.clock()
                pop_by_region = arcpy.gp.ZonalStatisticsAsTable_sa(admin,
                                                           'Name',
                                                           pop_not_served,
                                                           r"in_memory\pop{}".format(name),
                                                           '', 'SUM')
                arcpy.AddMessage("Zonal Stats took: {:.2f} seconds".format(time.clock() - start))
                pop_dict = dict()
                with arcpy.da.SearchCursor(pop_by_region, ['Name', 'SUM']) as cursor:
                    for row in cursor:
                        pop_dict[row[0]] = row[1]
                with arcpy.da.UpdateCursor(admin, ['Name', 'Rural_Pop_{}'.format(name)],
                                                  "{} = '{}'".format(query_type, country)
                                                  ) as cursor:
                    for row in cursor:
                        try:
                            row[1] = pop_dict[row[0]]
                            cursor.updateRow(row)
                        except KeyError:
                            pass
def execute ( self , parameters , messages ) : admin = join ( dirname ( __file__ ) , "Data" , "ToolData.gdb" , "Admin" ) scratch = tempfile . mkdtemp ( ) gdb = arcpy . CreateFileGDB_management ( scratch , "temp" ) . getOutput ( 0 ) country = parameters [ 0 ] . valueAsText if len ( country ) > 2 : query_type = 'Country' else : query_type = 'cc' mask = setEnvironment ( country , query_type ) cell_size = '0.0008333' start = time . clock ( ) area_served = arcpy . PolygonToRaster_conversion ( join ( dirname ( __file__ ) , "Data" , "ToolData.gdb" , "Urban" ) , 'RANK' , join ( gdb , 'served' ) , 'CELL_CENTER' , 'NONE' , cell_size ) area_not_served = arcpy . gp . IsNull ( area_served ) arcpy . AddMessage ( "Rasterize took: {:.2f} seconds" . format ( time . clock ( ) - start ) ) start = time . clock ( ) with open ( join ( dirname ( __file__ ) , "Data" , "Paths.txt" ) ) as paths_ref : for line in paths_ref : parts = line . split ( '...' ) name = parts [ 0 ] pop_grid = parts [ 1 ] pop_not_served = arcpy . sa . Con ( area_not_served , pop_grid , '0' , 'Value>0' ) arcpy . AddMessage ( "Con took: {:.2f} seconds" . format ( time . clock ( ) - start ) ) start = time . clock ( ) pop_by_region = arcpy . gp . ZonalStatisticsAsTable_sa ( admin , 'Name' , pop_not_served , r"in_memory\pop{}" . format ( name ) , '' , 'SUM' ) arcpy . AddMessage ( "Zonal Stats took: {:.2f} seconds" . format ( time . clock ( ) - start ) ) pop_dict = dict ( ) with arcpy . da . SearchCursor ( pop_by_region , [ 'Name' , 'SUM' ] ) as cursor : for row in cursor : pop_dict [ row [ 0 ] ] = row [ 1 ] with arcpy . da . UpdateCursor ( admin , [ 'Name' , 'Rural_Pop_{}' . format ( name ) ] , "{} = '{}'" . format ( query_type , country ) ) as cursor : for row in cursor : try : row [ 1 ] = pop_dict [ row [ 0 ] ] cursor . updateRow ( row ) except KeyError : pass
Calculates rural population in each administrative area.
Calculates rural population in each administrative area .
"""Calculates rural population in each administrative area.""" #set up a scratch workspace and set it as env #arcpy.env.scratchGDB = gdb #arcpy.env.workspace = gdb #arcpy.env.scratchWorkspace = gdb # arcpy.env.snapRaster = pop_grid # Get path to population data #try: #except: #   arcpy.AddError("ERROR: No {} data available in this region".format(name))
self
parameters
messages