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

        # Get Paramters
        global scratch
        scratch = tempfile.mkdtemp()
        country = parameters[0].valueAsText
        buff_dist = parameters[1].valueAsText
        pop_grid = parameters[2].value
        out_path = "in_memory\ServiceOverview"

        # Query WPDx database
        query_response, mask = queryWPDx(country)
        if mask == "Error":
            sys.exit(1)
        pnts = getWaterPoints(query_response)

        # Calculate percentage of population unserved in each administrative area
        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))
                       # would buffer be faster in different coordinate system?
        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)
        pop_dict = self.calcUnserved(mask, pop_not_served)
        output = arcpy.CopyFeatures_management(mask, out_path)

        # Append new data to output feature class
        with arcpy.da.UpdateCursor(output, ['Name', 'Pop_Unserved']) as cursor:
            for row in cursor:
                try:
                    row[1] = pop_dict[row[0]]
                except KeyError:
                    row[1] = 0
                    # this means if we have no population data for a region, it gets 100% served (not ideal)
                    # if Rural_Pop is not pre-calculated, Percent_Served = 0. Would be better to throw an error.
                cursor.updateRow(row)
        arcpy.CalculateField_management(output, 'Percent_Served',
                                        'round(1-!Pop_Unserved!/!Rural_Pop_{}!,2)'.format(pop_grid),
                                        'Python')
        arcpy.CalculateField_management(output, 'Percent_Served', 'max(0, !Percent_Served!)', 'PYTHON_9.3')

        parameters[3] = output
        parameters[4].value = self.outputCSV(country, output)
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()
        country = parameters[0].valueAsText
        buff_dist = parameters[1].valueAsText
        pop_grid = parameters[2].value
        out_path = "in_memory\ServiceOverview"
        query_response, mask = queryWPDx(country)
        if mask == "Error":
            sys.exit(1)
        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))
        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)
        pop_dict = self.calcUnserved(mask, pop_not_served)
        output = arcpy.CopyFeatures_management(mask, out_path)
        with arcpy.da.UpdateCursor(output, ['Name', 'Pop_Unserved']) as cursor:
            for row in cursor:
                try:
                    row[1] = pop_dict[row[0]]
                except KeyError:
                    row[1] = 0
                cursor.updateRow(row)
        arcpy.CalculateField_management(output, 'Percent_Served',
                                        'round(1-!Pop_Unserved!/!Rural_Pop_{}!,2)'.format(pop_grid),
                                        'Python')
        arcpy.CalculateField_management(output, 'Percent_Served', 'max(0, !Percent_Served!)', 'PYTHON_9.3')
        parameters[3] = output
        parameters[4].value = self.outputCSV(country, output)
def execute ( self , parameters , messages ) : global scratch scratch = tempfile . mkdtemp ( ) country = parameters [ 0 ] . valueAsText buff_dist = parameters [ 1 ] . valueAsText pop_grid = parameters [ 2 ] . value out_path = "in_memory\ServiceOverview" query_response , mask = queryWPDx ( country ) if mask == "Error" : sys . exit ( 1 ) 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 ) ) 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 ) pop_dict = self . calcUnserved ( mask , pop_not_served ) output = arcpy . CopyFeatures_management ( mask , out_path ) with arcpy . da . UpdateCursor ( output , [ 'Name' , 'Pop_Unserved' ] ) as cursor : for row in cursor : try : row [ 1 ] = pop_dict [ row [ 0 ] ] except KeyError : row [ 1 ] = 0 cursor . updateRow ( row ) arcpy . CalculateField_management ( output , 'Percent_Served' , 'round(1-!Pop_Unserved!/!Rural_Pop_{}!,2)' . format ( pop_grid ) , 'Python' ) arcpy . CalculateField_management ( output , 'Percent_Served' , 'max(0, !Percent_Served!)' , 'PYTHON_9.3' ) parameters [ 3 ] = output parameters [ 4 ] . value = self . outputCSV ( country , output )
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.""" # Get Paramters # Query WPDx database # Calculate percentage of population unserved in each administrative area # would buffer be faster in different coordinate system? # Append new data to output feature class # this means if we have no population data for a region, it gets 100% served (not ideal) # if Rural_Pop is not pre-calculated, Percent_Served = 0. Would be better to throw an error.
self
parameters
messages