Beispiel #1
0
def compute_hotspots(city_id):
    print 'Computing Moran index for', city_id
    shpfile = os.path.join(LATLNGS_SHP_DIR, str(city_id) + '.shp')
    moran_index = arcpy.SpatialAutocorrelation_stats(
        shpfile, 'price', 'NO_REPORT', 'INVERSE_DISTANCE_SQUARED',
        'EUCLIDEAN DISTANCE', 'NONE', '100', '#')
    print 'FINISHED %s!' % city_id
    return moran_index
Beispiel #2
0
        ols = arcpy.OrdinaryLeastSquares_stats(
            "C:\\MyUW\\SummerProject1\\censusjoin.shp", "OID_",
            "C:\\MyUW\\SummerProject1\\olsResults.shp", "canrate", "MEAN")

        # Create Spatial Weights Matrix (Can be based off input or output FC)
        # Process: Generate Spatial Weights Matrix...
        swm = arcpy.GenerateSpatialWeightsMatrix_stats(
            "C:\\MyUW\\SummerProject1\\censusjoin.shp", "OID_",
            "C:\\MyUW\\SummerProject1\\euclidean6Neighs.swm",
            "K_NEAREST_NEIGHBORS", "#", "#", "#", 6)

        # Calculate Moran's Index of Spatial Autocorrelation for
        # OLS Residuals using a SWM File.
        # Process: Spatial Autocorrelation (Morans I)...
        moransI = arcpy.SpatialAutocorrelation_stats(
            "C:\\MyUW\\SummerProject1\\olsResults.shp", "Residual",
            "NO_REPORT", "GET_SPATIAL_WEIGHTS_FROM_FILE", "EUCLIDEAN_DISTANCE",
            "NONE", "#", "C:\\MyUW\\SummerProject1\\euclidean6Neighs.swm")

    except:
        # If an error occurred when running the tool, print out the error message.
        print(arcpy.GetMessages())

    # Setup a new map document for the OLS results
    mxdols = arcpy.mapping.MapDocument(r"C:\\MyUW\\SummerProject1\\olsmap.mxd")
    dfols = arcpy.mapping.ListDataFrames(mxdols, "Layers")[0]

    # Make the OLS output a layer and add it to the mxd
    # Also give the layer the symbology of an existing layer
    olsResults = arcpy.mapping.Layer(
        r"C:\\MyUW\\SummerProject1\\olsResults.shp")
    olsSymbology = "C:\\MyUW\\SummerProject1\\ols_symbology.lyr"
Beispiel #3
0
tempEnvironment42 = arcpy.env.mask
arcpy.env.mask = ""
tempEnvironment43 = arcpy.env.spatialGrid3
arcpy.env.spatialGrid3 = "0"
tempEnvironment44 = arcpy.env.maintainSpatialIndex
arcpy.env.maintainSpatialIndex = "false"
tempEnvironment45 = arcpy.env.workspace
arcpy.env.workspace = "C:\\Users\\sfrazier\\Documents\\ArcGIS\\Default.gdb"
tempEnvironment46 = arcpy.env.MResolution
arcpy.env.MResolution = ""
tempEnvironment47 = arcpy.env.derivedPrecision
arcpy.env.derivedPrecision = "HIGHEST"
tempEnvironment48 = arcpy.env.ZTolerance
arcpy.env.ZTolerance = ""
arcpy.SpatialAutocorrelation_stats(OLSResults_shp, "Residual",
                                   "GENERATE_REPORT", "INVERSE_DISTANCE",
                                   "EUCLIDEAN_DISTANCE", "NONE", "", "")
arcpy.env.newPrecision = tempEnvironment0
arcpy.env.autoCommit = tempEnvironment1
arcpy.env.XYResolution = tempEnvironment2
arcpy.env.processingServerUser = tempEnvironment3
arcpy.env.XYDomain = tempEnvironment4
arcpy.env.processingServerPassword = tempEnvironment5
arcpy.env.scratchWorkspace = tempEnvironment6
arcpy.env.cartographicPartitions = tempEnvironment7
arcpy.env.terrainMemoryUsage = tempEnvironment8
arcpy.env.MTolerance = tempEnvironment9
arcpy.env.compression = tempEnvironment10
arcpy.env.coincidentPoints = tempEnvironment11
arcpy.env.randomGenerator = tempEnvironment12
arcpy.env.outputCoordinateSystem = tempEnvironment13
##    # Growth as a function of {log of starting income, dummy for South
##    # counties, interaction term for South counties, population density}
##    # Process: Ordinary Least Squares... 
##    ols = arcpy.OrdinaryLeastSquares_stats("USCounties.shp", "MYID", 
##                        "olsResults.shp", "GROWTH",
##                        "LOGPCR69;SOUTH;LPCR_SOUTH;PopDen69",
##                        "olsCoefTab.dbf",
##                        "olsDiagTab.dbf")
##                        
##
##    # Calculate Moran's I Index of Spatial Autocorrelation for 
##    # OLS Residuals using a SWM File.  
##    # Process: Spatial Autocorrelation (Morans I)...

moransI = arcpy.SpatialAutocorrelation_stats(r"C:\Users\sfrazier\Desktop\Geog777\Project1\olsResults1.shp",
                                             "Residual", "GENERATE_REPORT", "INVERSE_DISTANCE",
                                             "EUCLIDEAN_DISTANCE", "NONE", "","")

print ("at the bottom")
####except:
####    # If an error occurred when running the tool, print out the error message.
####    print(arcpy.GetMessages())


#-------------------------------------------------------------------------------------------    

##refLyr = m.listLayers("Points of Interest")[0]
##m.insertLayer(refLyr, insertLyr, "BEFORE")
#aprx.saveACopy(r"C:\Projects\YosemiteNP\Yosemite_updated.aprx")
#aprx.save()
del aprx
Beispiel #5
0
def run(*args):
    try:

        #setup workspace
        env.workspace = r"C:\Users\sfrazier\Desktop\GEOG777\Project1"
        env.overwriteOutput = True

        #Set arcGIS Pro project location - will be used throughout script.
        aprx = arcpy.mp.ArcGISProject(
            r"C:\Users\sfrazier\Desktop\GEOG777\Project1\Project1\Project1.aprx"
        )

        # Set local variables for the IDW tool
        inPointFeatures = r"\well_nitrate\well_nitrate_WTM.shp"
        zField = "nitr_ran"
        ##cellSize = 2000.0
        power = float(powerEntry.get())
        searchRadius = RadiusVariable(10, )
        powerText = str(power)
        messagebox.showinfo("Running", "The analysis has begun using "+powerText+" for the power\n lease"\
                            " wait for the IDW process to complete.")

        # Execute IDW
        outIDW = Idw(inPointFeatures, zField, '', power, searchRadius)

        ## Save the output IDW as a Tiff
        outIDW.save(
            r"C:\Users\sfrazier\Desktop\GEOG777\Project1\outputFiles\idwout.tif"
        )

        #Set variables for building the IDW jpeg map
        idwTif = r"C:\Users\sfrazier\Desktop\GEOG777\Project1\outputFiles\idwout.tif"

        # Locating the first map in the project
        map1 = aprx.listMaps('IDWMap')[0]
        map1.addDataFromPath(
            r"C:\Users\sfrazier\Desktop\GEOG777\Project1\outputFiles\idwout.tif"
        )

        #define the Layers
        refLay = map1.listLayers()[0]
        #print("no name: " +refLay.name)
        moveLay = map1.listLayers()[1]
        #print(moveLay.name)

        #Apply symbology to the IDW raster
        sybLyr = r"C:\Users\sfrazier\Desktop\Geog777\Project1\outputFiles\idwout.lyrx"

        #apply symbology for the output raster
        arcpy.ApplySymbologyFromLayer_management(refLay, sybLyr)

        #rearrange the layers in the map to move the tracts above the raster.
        map1.moveLayer(refLay, moveLay, 'BEFORE')

        #locate correct layout to export the map
        lyt = aprx.listLayouts()[1]
        lyt.exportToJPEG(
            r"C:\Users\sfrazier\Desktop\Geog777\Project1\Images\Idw.jpg")

        #message box to let user know process has completed the IDW
        messagebox.showinfo(
            "Completed IDW",
            "Completed IDW Interpolation.\n OLS process is now running.")

        #activate display IDW map button
        idwDisplay.config(state=NORMAL)

        # Set local variables for zonal join
        idwTif = r"C:\Users\sfrazier\Desktop\GEOG777\Project1\outputFiles\idwout.tif"
        inZoneData = r"cancer_tracts\cancer_tracts_WTM.shp"
        zoneField = "GEOID10"
        inValueRaster = idwTif
        outTable = "zonalstat"

        # Execute ZonalStatisticsAsTable
        outZSaT = ZonalStatisticsAsTable(inZoneData, zoneField, inValueRaster,
                                         outTable, "DATA", "MEAN")

        #set the environment setting
        arcpy.env.qualifiedFieldNames = False

        # Set local variables for the zonal table to census tracts shapefile
        inFeatures = r"\cancer_tracts\cancer_tracts_WTM.shp"
        joinTable = "zonalstat"
        joinField = "GEOID10"
        outFeature = r"outputFiles\Tract_IDW"

        # Join the feature layer to a table
        canTract_joined_table = arcpy.AddJoin_management(
            inFeatures, joinField, joinTable, joinField, "KEEP_COMMON")

        # Copy the layer to a new permanent feature class
        arcpy.CopyFeatures_management(canTract_joined_table, outFeature)

        print("join")

        #Location of the Tract IDW.shp
        feature = r"C:\Users\sfrazier\Desktop\Geog777\Project1\outputFiles\Tract_IDW.shp"

        #run the OLS tool
        ols = arcpy.OrdinaryLeastSquares_stats(feature, "Rowid_",
                                               r"outputFiles\olsResults.shp",
                                               "canrate", "MEAN", '', '',
                                               r"outputFiles\olsTable.pdf")
        print("we got to the OLS")

        #Load data into OLS Map
        olsRes = r"C:\Users\sfrazier\Desktop\Geog777\Project1\outputFiles\olsResults.shp"
        map2 = aprx.listMaps("OLS")[0]
        map2.addDataFromPath(olsRes)

        #call the ols layout and export the map
        lyt1 = aprx.listLayouts("olsMap")[0]
        lyt1.exportToJPEG(
            r"C:\Users\sfrazier\Desktop\Geog777\Project1\Images\olsMap.jpg")

        #activate the olsDisplay button and show a feedback that OLS is completed
        olsDisplay.config(state=NORMAL)
        messagebox.showinfo(
            "Completed OLS",
            "Completed OLS. \nPlease wait for process to complete")

        # run morans I
        moransI = arcpy.SpatialAutocorrelation_stats(
            r"C:\Users\sfrazier\Desktop\Geog777\Project1\outputFiles\olsResults.shp",
            "Residual", "GENERATE_REPORT", "INVERSE_DISTANCE",
            "EUCLIDEAN_DISTANCE", "NONE", "", "")
        #print("completed spatial Process")

        # activate ols button
        olsDisplay.config(state=NORMAL)

        #let user know that process is complete.
        messagebox.showinfo("Process Completed", "Process Completed")

    except:
        #an error check to let user know to enter a number.
        messagebox.showwarning(
            "Power Error", "Enter a number greater then 1\n for the power")
        print("please enter a number value")
# Skipped OLS for now (done by hand earlier). Will revisit. 

# Moran's I
# Where Moran's I evaluates whether a pattern is clustered, dispered, or 
# random.
# Positive Moran's I values, a p-value less than the desired confidence level and an abs(z-score) greater than the related p-value z score suggests strong  spatial autocorrelation and a rejection of the null hypothesis.

arcpy.env.workspace = workspace + r'\\MAUP.gdb'
arcpy.env.overwriteOutput = True

in_layers = ['VanDA_OLS', 'VanCT_OLS']
field = 'Residual'
out_html_list = []
os.chdir(workspace)
for layer in in_layers:
    arcpy.SpatialAutocorrelation_stats(layer, field, 'GENERATE_REPORT', 'INVERSE_DISTANCE', 'EUCLIDEAN_DISTANCE', 'NONE', '#')

    desc = arcpy.Describe(layer)
    layer_name = desc.nameString
    out_html_name = 'MoI_{}.html'.format(layer_name)
    os.rename(glob.glob(workspace + r'\\MoransI*.html')[0], out_html_name)
    out_html_list.append(out_html_name)
    print('Completed layer {}'.format(layer_name))

for doc in out_html_list:
    soup = BeautifulSoup(open(workspace + r'\\' + doc), 'html.parser')
    table = soup.find('table', attrs={'id':'keytable'})
    print(table.get_text())

# Layout: This is better done with ArcMap's GUI. Could potentially revisit creating this map programatically, but the script will likely be pretty extensive.
Beispiel #7
0
def idw():
    #Determine which spatial unit the user chose
    selection = unitVar.get()

    #Setting up the workspace
    arcpy.env.workspace = "C:\\MAMP\\htdocs\\cancerAnalysis\\files"
    arcpy.env.overwriteOutput = True
    arcpy.CheckOutExtension("Spatial")

    #Setting up the variables for the IDW tool
    inPoint = "well_nitrate.shp"
    zField = "nitr_ran"
    power = float(powerEntry.get())
    print power
    searchRadius = RadiusVariable(10, 150000)

    #Run IDW
    idwOutPut = Idw(inPoint, zField, "", power, searchRadius)

    #Save the output raster
    idwOutPut.save("C:\\MAMP\\htdocs\\cancerAnalysis\\project1\\idw.tif")

    #Update the mxd to include the IDW raster
    mxd = arcpy.mapping.MapDocument(r"C:\\MAMP\\htdocs\\cancerAnalysis\\idwMap.mxd")
    dataFrame = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

    #Change the symbology of the IDW raster
    idwTiff = arcpy.mapping.Layer(r"C:\\MAMP\\htdocs\\cancerAnalysis\\project1\\idw.tif")
    idwSymbology = r"C:\\MAMP\\htdocs\\cancerAnalysis\\project1\\idwSymbology.lyr"
    arcpy.ApplySymbologyFromLayer_management(idwTiff, idwSymbology)

    #Add newly changed IDW raster to the map
    arcpy.mapping.AddLayer(dataFrame, idwTiff, "BOTTOM")

    #Export the map
    arcpy.mapping.ExportToPNG(mxd, "C:\\MAMP\\htdocs\\cancerAnalysis\\idwResults.png")
    del mxd

    #Update the county mxd to include the IDW raster
    mxd2 = arcpy.mapping.MapDocument(r"C:\\MAMP\\htdocs\\cancerAnalysis\\countyIDWMap.mxd")
    dataFrame2 = arcpy.mapping.ListDataFrames(mxd2, "Layers")[0]
    arcpy.mapping.AddLayer(dataFrame2, idwTiff, "BOTTOM")
    arcpy.mapping.ExportToPNG(mxd2, "C:\\MAMP\\htdocs\\cancerAnalysis\\countyIDWResults.png")
    idwDisplay.config(state=NORMAL)
    tkMessageBox.showinfo("Completed IDW", "Completed IDW interpolation...")

    #Setting up the process for the two different spatial units
    if selection == 1:
        #Allow overwriting
        arcpy.env.overwriteOutput = True

        #Setting up the variables for the Zonal Statistics tool
        inZone = "cancer_tracts.shp"
        zone = "GEOID10"
        rasterValue = idwTiff
        table = "zonalStatistics.dbf"

        #Run the Zonal Statistics tool
        outZoneStat = ZonalStatisticsAsTable(inZone, zone, rasterValue, table, "NODATA", "MEAN")

        #Join the Zonal Statistics table with the census tracts
        try:
            #Setting up the variables
            inFeatures = "cancer_tracts.shp"
            layerName = "cancer_tracts"
            joinTable = "zonalStatistics.dbf"
            joinField = "GEOID10"
            outputFeature = r"C:\\MAMP\\htdocs\\cancerAnalysis\\files\\Result_Testing.gdb\\joinedCensus"

            #Make a feature layer from the cancer tracts
            arcpy.MakeFeatureLayer_management(inFeatures, layerName)

            #Join the feature to the table
            arcpy.AddJoin_management(layerName, joinField, joinTable, joinField, "KEEP_COMMON")

            #Create a copy of the joined layer
            arcpy.CopyFeatures_management(layerName, outputFeature)
            tkMessageBox.showinfo("Completed Zonal Statistics", "Completed Zonal Statistics...")
        except Exception, e:
            import traceback, sys
            tb = sys.exc_info()[2]
            print "Line %i" % tb.tb_lineno
            print e.message

        #Run linear regression on the results
        try:
            #Set the environment to allow to overwrite existing data
            arcpy.env.overwriteOutput = True

            #Create the tool to run Ordinary Least Squares
            ordLeastSq = arcpy.OrdinaryLeastSquares_stats("C:\\MAMP\\htdocs\\cancerAnalysis\\files\\Result_Testing.gdb\\joinedCensus",
            "cancer_tracts_ID", "C:\\MAMP\\htdocs\\cancerAnalysis\\files\\Result_Testing.gdb\\olsResults", "cancer_tracts_canrate",
            "zonalStatistics_MEAN", "", "C:\\MAMP\\htdocs\\cancerAnalysis\\project1\\olsTable.pdf")
        except:
            print (arcpy.GetMessages())

        moransI = arcpy.SpatialAutocorrelation_stats("C:\\MAMP\\htdocs\\cancerAnalysis\\files\\Result_Testing.gdb\\olsResults",
        "Residual", "NO_REPORT", "INVERSE_DISTANCE", "EUCLIDEAN_DISTANCE", "NONE", "", "")

        #Get the mxd
        olsMXD = arcpy.mapping.MapDocument("C:\\MAMP\\htdocs\\cancerAnalysis\\OLSmap.mxd")
        olsDF = arcpy.mapping.ListDataFrames(olsMXD, "Layers")[0]

        #Add the OLS output to an mxd and apply symbology
        olsResults = arcpy.mapping.Layer(r"C:\\MAMP\\htdocs\\cancerAnalysis\\files\\Result_Testing.gdb\\olsResults")
        olsSymbology = r"C:\\MAMP\\htdocs\\cancerAnalysis\\project1\\olsSymbology.lyr"
        arcpy.ApplySymbologyFromLayer_management(olsResults, olsSymbology)
        arcpy.mapping.AddLayer(olsDF, olsResults, "AUTO_ARRANGE")

        #Add the legend to the map
        olsLegend = arcpy.mapping.ListLayoutElements(olsMXD, "LEGEND_ELEMENT", "Legend")[0]
        olsLegend.autoAdd = True

        #Export the mxd
        arcpy.mapping.ExportToPNG(olsMXD, "C:\\MAMP\\htdocs\\cancerAnalysis\\olsResults.png")
        tkMessageBox.showinfo("Completed OLS", "Completed Ordinary Least Squares..." + "\n\nYou can now view and download the maps")
        olsDisplay.config(state=NORMAL)
        downloadButton.config(state=NORMAL)
        image3 = ImageTk.PhotoImage(file="C:\\MAMP\\htdocs\\cancerAnalysis\\olsResults.png")
        mapDisplay.configure(image=image3)
        mapDisplay.image = image3
        Image.ANTIALIAS
Beispiel #8
0
            print "Line %i" % tb.tb_lineno
            print e.message

        #Run linear regression on the results
        try:
            #Set the environment to allow to overwrite existing data
            arcpy.env.overwriteOutput = True

            #Create the tool to run Ordinary Least Squares
            ordLeastSq = arcpy.OrdinaryLeastSquares_stats("C:\\MAMP\\htdocs\\cancerAnalysis\\files\\Result_Testing.gdb\\joinedCounty",
            "zonalStatistics_ZONE_CODE", "C:\\MAMP\\htdocs\\cancerAnalysis\\files\\Result_Testing.gdb\\countyOLSResults", "cancer_county_canrate",
            "zonalStatistics_MEAN", "", "C:\\MAMP\\htdocs\\cancerAnalysis\\project1\\countyOLSTable.pdf")
        except:
            print (arcpy.GetMessages())

        moransII = arcpy.SpatialAutocorrelation_stats("C:\\MAMP\\htdocs\\cancerAnalysis\\files\\Result_Testing.gdb\\countyOLSResults",
        "Residual", "NO_REPORT", "INVERSE_DISTANCE", "EUCLIDEAN_DISTANCE", "NONE", "", "")

        #Get the mxd
        olsMXD = arcpy.mapping.MapDocument("C:\\MAMP\\htdocs\\cancerAnalysis\\OLSmap.mxd")
        olsDF = arcpy.mapping.ListDataFrames(olsMXD, "Layers")[0]

        #Add the OLS output to an mxd and apply symbology
        olsResults = arcpy.mapping.Layer(r"C:\\MAMP\\htdocs\\cancerAnalysis\\files\\Result_Testing.gdb\\countyOLSResults")
        olsSymbology = r"C:\\MAMP\\htdocs\\cancerAnalysis\\project1\\olsSymbology.lyr"
        arcpy.ApplySymbologyFromLayer_management(olsResults, olsSymbology)
        arcpy.mapping.AddLayer(olsDF, olsResults, "AUTO_ARRANGE")

        #Add the legend to the map
        olsLegend = arcpy.mapping.ListLayoutElements(olsMXD, "LEGEND_ELEMENT", "Legend")[0]
        olsLegend.autoAdd = True