WPDx_Toolset.pyt
def execute(self, parameters, messages):
        """Calculates percentage of population unserved in each administrative area."""
        #scratchworkspace = "in_memory"

        # Get Paramters
        global scratch
        scratch = tempfile.mkdtemp()
        zone = parameters[0].valueAsText
        num = parameters[1].valueAsText
        buff_dist = parameters[2].valueAsText
        pop_grid = parameters[3].value
        out_path = parameters[4].value

        # Query WPDx database
        query_response, mask = queryWPDx(zone)
        pnts = getWaterPoints(query_response)

        start = time.clock()
        pnts_func = arcpy.MakeFeatureLayer_management(pnts, 'Functioning',
                                                      "status_id='yes'")

        pnts_buff = arcpy.Buffer_analysis(pnts_func, r"in_memory\buffer", "{} Meters".format(buff_dist))
        if mask == "Error":
            arcpy.env.extent = arcpy.Describe(pnts_buff).extent
        arcpy.AddMessage("Buffer took: {:.2f} seconds".format(time.clock() - start))

        area_urban = join(dirname(__file__), "Data", "ToolData.gdb", "Urban")
        pop_not_served = getPopNotServed(pnts_buff, pop_grid, area_urban)

        with open(join(dirname(__file__), "Data", "Paths.txt")) as paths_ref:
            for line in paths_ref:
                parts = line.split('...')
                name = parts[0]
                if name == pop_grid:
                    cell_size = float(parts[2])*111000

        cell_factor = int(round(float(buff_dist) / cell_size))
        neighborhood = arcpy.sa.NbrCircle(cell_factor, "CELL")

        FocalStat = arcpy.sa.FocalStatistics(pop_not_served, neighborhood,
                                     "SUM", "DATA")

        agg = arcpy.sa.Aggregate(FocalStat, cell_factor, 'MAXIMUM')
        #arcpy.env.mask = mask
        agg_pnts = arcpy.RasterToPoint_conversion(agg, r"in_memory\agg_pnt", 'Value')
        sort = arcpy.Sort_management(agg_pnts, r"in_memory\sort", 'grid_code DESCENDING')
        top = arcpy.MakeFeatureLayer_management(sort, 'TOP', "OBJECTID<{}".format(num))
        arcpy.AlterField_management (top, "grid_code", "Pop_Served", "Pop_Served")
        output = arcpy.CopyFeatures_management(top, out_path)

        parameters[4] = output
        parameters[5].value = self.outputCSV(output, zone)

        # should zones close to broken points count as good locations for a new installation?
Calculates percentage of population unserved in each administrative area.
Calculates percentage of population unserved in each administrative area.
Calculates percentage of population unserved in each administrative area .
def execute(self, parameters, messages):
        global scratch
        scratch = tempfile.mkdtemp()
        zone = parameters[0].valueAsText
        num = parameters[1].valueAsText
        buff_dist = parameters[2].valueAsText
        pop_grid = parameters[3].value
        out_path = parameters[4].value
        query_response, mask = queryWPDx(zone)
        pnts = getWaterPoints(query_response)
        start = time.clock()
        pnts_func = arcpy.MakeFeatureLayer_management(pnts, 'Functioning',
                                                      "status_id='yes'")
        pnts_buff = arcpy.Buffer_analysis(pnts_func, r"in_memory\buffer", "{} Meters".format(buff_dist))
        if mask == "Error":
            arcpy.env.extent = arcpy.Describe(pnts_buff).extent
        arcpy.AddMessage("Buffer took: {:.2f} seconds".format(time.clock() - start))
        area_urban = join(dirname(__file__), "Data", "ToolData.gdb", "Urban")
        pop_not_served = getPopNotServed(pnts_buff, pop_grid, area_urban)
        with open(join(dirname(__file__), "Data", "Paths.txt")) as paths_ref:
            for line in paths_ref:
                parts = line.split('...')
                name = parts[0]
                if name == pop_grid:
                    cell_size = float(parts[2])*111000
        cell_factor = int(round(float(buff_dist) / cell_size))
        neighborhood = arcpy.sa.NbrCircle(cell_factor, "CELL")
        FocalStat = arcpy.sa.FocalStatistics(pop_not_served, neighborhood,
                                     "SUM", "DATA")
        agg = arcpy.sa.Aggregate(FocalStat, cell_factor, 'MAXIMUM')
        agg_pnts = arcpy.RasterToPoint_conversion(agg, r"in_memory\agg_pnt", 'Value')
        sort = arcpy.Sort_management(agg_pnts, r"in_memory\sort", 'grid_code DESCENDING')
        top = arcpy.MakeFeatureLayer_management(sort, 'TOP', "OBJECTID<{}".format(num))
        arcpy.AlterField_management (top, "grid_code", "Pop_Served", "Pop_Served")
        output = arcpy.CopyFeatures_management(top, out_path)
        parameters[4] = output
        parameters[5].value = self.outputCSV(output, zone)
def execute ( self , parameters , messages ) : global scratch scratch = tempfile . mkdtemp ( ) zone = parameters [ 0 ] . valueAsText num = parameters [ 1 ] . valueAsText buff_dist = parameters [ 2 ] . valueAsText pop_grid = parameters [ 3 ] . value out_path = parameters [ 4 ] . value query_response , mask = queryWPDx ( zone ) pnts = getWaterPoints ( query_response ) start = time . clock ( ) pnts_func = arcpy . MakeFeatureLayer_management ( pnts , 'Functioning' , "status_id='yes'" ) pnts_buff = arcpy . Buffer_analysis ( pnts_func , r"in_memory\buffer" , "{} Meters" . format ( buff_dist ) ) if mask == "Error" : arcpy . env . extent = arcpy . Describe ( pnts_buff ) . extent arcpy . AddMessage ( "Buffer took: {:.2f} seconds" . format ( time . clock ( ) - start ) ) area_urban = join ( dirname ( __file__ ) , "Data" , "ToolData.gdb" , "Urban" ) pop_not_served = getPopNotServed ( pnts_buff , pop_grid , area_urban ) with open ( join ( dirname ( __file__ ) , "Data" , "Paths.txt" ) ) as paths_ref : for line in paths_ref : parts = line . split ( '...' ) name = parts [ 0 ] if name == pop_grid : cell_size = float ( parts [ 2 ] ) * 111000 cell_factor = int ( round ( float ( buff_dist ) / cell_size ) ) neighborhood = arcpy . sa . NbrCircle ( cell_factor , "CELL" ) FocalStat = arcpy . sa . FocalStatistics ( pop_not_served , neighborhood , "SUM" , "DATA" ) agg = arcpy . sa . Aggregate ( FocalStat , cell_factor , 'MAXIMUM' ) agg_pnts = arcpy . RasterToPoint_conversion ( agg , r"in_memory\agg_pnt" , 'Value' ) sort = arcpy . Sort_management ( agg_pnts , r"in_memory\sort" , 'grid_code DESCENDING' ) top = arcpy . MakeFeatureLayer_management ( sort , 'TOP' , "OBJECTID<{}" . format ( num ) ) arcpy . AlterField_management ( top , "grid_code" , "Pop_Served" , "Pop_Served" ) output = arcpy . CopyFeatures_management ( top , out_path ) parameters [ 4 ] = output parameters [ 5 ] . value = self . outputCSV ( output , zone )
Calculates percentage of population unserved in each administrative area.
Calculates percentage of population unserved in each administrative area .
"""Calculates percentage of population unserved in each administrative area.""" #scratchworkspace = "in_memory" # Get Paramters # Query WPDx database #arcpy.env.mask = mask # should zones close to broken points count as good locations for a new installation?
self
parameters
messages