Пример #1
0
def EBK_ga(inPointFeatures, outPath):
    try:
        arcpy.AddField_management(inPointFeatures, "PM102", "FLOAT", 9)
    except:
        traceback.print_exc()
    try:
        arcpy.CalculateField_management(inPointFeatures, 'PM102',
                                        "float(!PM10__μg_!)", "PYTHON_9.3")
    except:
        traceback.print_exc()

    name = os.path.split(inPointFeatures)[1][:-4]
    outRaster = os.path.join(outPath, name + '.tif')
    cellSize = 0.01
    transformation = "NONE"
    maxLocalPoints = 50
    overlapFactor = 0.5
    numberSemivariograms = 100
    # Set variables for search neighborhood
    radius = 1.0
    smooth = 0.14
    try:
        #lyr to shp
        searchNeighbourhood = arcpy.SearchNeighborhoodSmoothCircular(
            radius, smooth)
    except Exception as err:
        arcpy.AddMessage("SearchNeighborhoodSmoothCircular: " + " Failed")
        arcpy.AddMessage(err.message)
    outputType = "PREDICTION"
    Output_geostatistical_layer = ''
    quantileValue = ""
    thresholdType = ""
    probabilityThreshold = ""
    semivariogram = "POWER"
    tempEnvironment0 = arcpy.env.extent
    arcpy.env.extent = Extent
    # Execute EmpiricalBayesianKriging
    start = time.time()
    try:
        arcpy.EmpiricalBayesianKriging_ga(
            inPointFeatures, 'PM102', Output_geostatistical_layer, outRaster,
            cellSize, transformation, maxLocalPoints, overlapFactor,
            numberSemivariograms, searchNeighbourhood, outputType,
            quantileValue, thresholdType, probabilityThreshold)
        print('Converting {} to {}'.format(inPointFeatures, outRasNa))
        arcpy.AddMessage(normaltime + ":" + "经验贝叶斯克里金插值完成")
    except Exception as err:
        arcpy.AddMessage("EmpiricalBayesianKriging_ga: " + " Failed")
        arcpy.AddMessage(err.message)
    end = time.time()
    print('Running time: %s Seconds' % (end - start))
    arcpy.env.extent = tempEnvironment0
Пример #2
0
 def runInterpolationOnGroupLayer(self, groupKey):
     try:
         EBK = arcpy.EmpiricalBayesianKriging_ga(
             in_features=self.nameOfLayerOut(groupKey),
             z_field=self.elevationField,
             out_ga_layer="",
             out_raster=makeFullPath(tempGDB, self.nameOfEBKOut(groupKey)),
             cell_size="",
             transformation_type="NONE",
             max_local_points="100",
             overlap_factor="1",
             number_semivariograms="100",
             search_neighborhood="",
             output_type="PREDICTION",
             quantile_value="0.5",
             threshold_type="EXCEED",
             probability_threshold="",
             semivariogram_model_type="POWER")
     except Exception:
         pass
                                       "MEAN_vapor_pressure", "RASTERVALU",
                                       scratchGDB + "/coef_table", "", "")
#arcpy.TableToTable_conversion(scratchGDB + "/coef_table", "in_memory", "coef_table")
#arcpy.Delete_management(scratchGDB + "/coef_table")
intercept = list(
    (row.getValue("Coef")
     for row in arcpy.SearchCursor(scratchGDB +
                                   "/coef_table", fields="Coef")))[0]
slope = list(
    (row.getValue("Coef")
     for row in arcpy.SearchCursor(scratchGDB +
                                   "/coef_table", fields="Coef")))[1]

#Estimate air temperature at all stations using elevation, and slope and intercept from above
arcpy.AddMessage("Estimating vapor pressure at all stations")
cursor = arcpy.UpdateCursor(fcStations_wElevation)
for row in cursor:
    if str(row.getValue("MEAN_vapor_pressure")) == "None":
        row.setValue("MEAN_vapor_pressure",
                     (row.getValue("RASTERVALU") * slope) + intercept)
        cursor.updateRow(row)
del cursor
del row

#Run empirical bayesian kriging
arcpy.AddMessage("Running Empirical Bayesian Kriging")
arcpy.EmpiricalBayesianKriging_ga(in_features=fcStations_wElevation, z_field="MEAN_vapor_pressure", out_ga_layer="#", \
    out_raster=outRaster, cell_size=output_cell_size, transformation_type="EMPIRICAL", max_local_points="100", overlap_factor="1", \
    number_semivariograms="100", search_neighborhood="NBRTYPE=SmoothCircular RADIUS=10000.9518700025 SMOOTH_FACTOR=0.2", \
    output_type="PREDICTION", quantile_value="0.5", threshold_type="EXCEED", probability_threshold="", semivariogram_model_type="WHITTLE_DETRENDED")
Пример #4
0
    def interpolate_wle(self):
        """
        Interpolates water level elevation, used as preliminary step for getting depth to groundwater and disconnected wetted areas.

        Args:
            self.method: 'Kriging', 'IDW', or 'Nearest Neighbor'. Determines the method used to interpolate WLE.

        Saves interpolated WLE raster to self.out_dir (also saves a WLE variance raster if Kriging method is used).
        """

        try:
            arcpy.CheckOutExtension('Spatial')  # check out license
            arcpy.gp.overwriteOutput = True
            arcpy.env.workspace = self.cache

            try:
                self.logger.info("Reading input rasters ...")
                ras_h = arcpy.Raster(self.path2h_ras)
                ras_dem = arcpy.Raster(self.path2dem_ras)
                arcpy.env.extent = ras_dem.extent
                cell_size = arcpy.GetRasterProperties_management(
                    ras_dem, 'CELLSIZEX').getOutput(0)
                self.logger.info("OK")
            except:
                self.logger.info(
                    "ERROR: Could not find / access input rasters.")
                return True

            try:
                self.logger.info("Making WSE raster ...")
                ras_wse = Con(ras_h > 0, (ras_dem + ras_h))
                temp_dem = Con(((ras_dem > 0) & IsNull(ras_h)), ras_dem)
                ras_dem = temp_dem
                self.logger.info("OK")
            except:
                self.logger.info("ERROR: Input rasters contain invalid data.")
                return True

            try:
                self.logger.info("Converting WSE raster to points ...")
                pts_wse = arcpy.RasterToPoint_conversion(
                    ras_wse, os.path.join(self.cache, "pts_wse.shp"))
                self.logger.info("OK")
            except arcpy.ExecuteError:
                self.logger.info(arcpy.AddError(arcpy.GetMessages(2)))
                return True
            except Exception as e:
                self.logger.info(arcpy.GetMessages(2))
                self.logger.info(e.args[0])
                return True

            if self.method == "Kriging":
                try:
                    self.logger.info("Ordinary Kriging interpolation ...")
                    # Spherical semivariogram using 12 nearest points to interpolate
                    ras_wle_dem = Kriging(in_point_features=pts_wse,
                                          z_field="grid_code",
                                          kriging_model=KrigingModelOrdinary(
                                              "Spherical", lagSize=cell_size),
                                          cell_size=cell_size,
                                          search_radius="Variable 12")
                    ras_wle_dem.save(os.path.join(self.cache, "ras_wle_dem"))
                    # out_variance_prediction_raster=os.path.join(self.cache, "ras_wle_var") ***
                    ras_wle_dem = arcpy.Raster(
                        os.path.join(self.cache, "ras_wle_dem"))
                    # ras_wle_var = arcpy.Raster(os.path.join(self.cache, "ras_wle_var")) ***
                    self.logger.info("OK")

                except arcpy.ExecuteError:
                    if "010079" in str(arcpy.GetMessages(2)):
                        self.logger.info(
                            "Could not fit semivariogram, increasing lag size and retrying..."
                        )
                        empty_bins = True
                        itr = 2
                        while empty_bins:
                            try:
                                ras_wle_dem = Kriging(
                                    in_point_features=pts_wse,
                                    z_field="grid_code",
                                    kriging_model=KrigingModelOrdinary(
                                        "Spherical", lagSize=cell_size * itr),
                                    cell_size=cell_size,
                                    search_radius="Variable 12")
                                try:
                                    ras_wle_dem.save(
                                        os.path.join(self.cache,
                                                     "ras_wle_dem"))
                                    # out_variance_prediction_raster=os.path.join(self.cache, "ras_wle_var") ***
                                    ras_wle_dem = arcpy.Raster(
                                        os.path.join(self.cache,
                                                     "ras_wle_dem"))
                                    # ras_wle_var = arcpy.Raster(os.path.join(self.cache, "ras_wle_var")) ***
                                    self.logger.info("OK")
                                    empty_bins = False
                                except:
                                    self.logger.info(
                                        "ERROR: Failed to produce interpolated raster."
                                    )
                                    return True
                            except:
                                self.logger.info(
                                    "Still could not fit semivariogram, increasing lag and retrying..."
                                )
                                itr *= 2
                                if itr > 16:
                                    break

                    self.logger.info(arcpy.AddError(arcpy.GetMessages(2)))
                    return True
                except Exception as e:
                    self.logger.info(arcpy.GetMessages(2))
                    self.logger.info(e.args[0])
                    return True

            elif self.method == "IDW":
                try:
                    self.logger.info("IDW interpolation...")
                    # using IDW power of 2 with 12 nearest neighbors
                    arcpy.Idw_3d(in_point_features=pts_wse,
                                 z_field="grid_code",
                                 out_raster=os.path.join(
                                     self.cache, "ras_wle_dem"),
                                 cell_size=cell_size,
                                 search_radius="Variable 12")
                    ras_wle_dem = arcpy.Raster(
                        os.path.join(self.cache, "ras_wle_dem"))
                    self.logger.info("OK")
                except arcpy.ExecuteError:
                    self.logger.info(arcpy.AddError(arcpy.GetMessages(2)))
                    return True
                except Exception as e:
                    self.logger.info(arcpy.GetMessages(2))
                    self.logger.info(e.args[0])
                    return True

            elif self.method == "Nearest Neighbor":
                try:
                    self.logger.info("Nearest Neighbor interpolation...")
                    # using IDW with 1 nearest neighbor
                    arcpy.Idw_3d(in_point_features=pts_wse,
                                 z_field="grid_code",
                                 out_raster=os.path.join(
                                     self.cache, "ras_wle_dem"),
                                 cell_size=cell_size,
                                 search_radius="Variable 1")
                    ras_wle_dem = arcpy.Raster(
                        os.path.join(self.cache, "ras_wle_dem"))
                    self.logger.info("OK")
                except arcpy.ExecuteError:
                    self.logger.info(arcpy.AddError(arcpy.GetMessages(2)))
                    return True
                except Exception as e:
                    self.logger.info(arcpy.GetMessages(2))
                    self.logger.info(e.args[0])
                    return True

            elif self.method == "EBK":
                try:
                    self.logger.info(
                        "Empirical Bayesian Kriging interpolation...")
                    search_nbrhood = arcpy.SearchNeighborhoodStandardCircular(
                        nbrMin=12, nbrMax=12)
                    arcpy.EmpiricalBayesianKriging_ga(
                        in_features=pts_wse,
                        z_field="grid_code",
                        out_raster=os.path.join(self.cache, "ras_wle_dem"),
                        cell_size=cell_size,
                        transformation_type="EMPIRICAL",
                        number_semivariograms=100,
                        search_neighborhood=search_nbrhood,
                        output_type="PREDICTION",
                        semivariogram_model_type="EXPONENTIAL")
                    ras_wle_dem = arcpy.Raster(
                        os.path.join(self.cache, "ras_wle_dem"))
                    self.logger.info("OK")
                except arcpy.ExecuteError:
                    self.logger.info(arcpy.AddError(arcpy.GetMessages(2)))
                    return True
                except Exception as e:
                    self.logger.info(arcpy.GetMessages(2))
                    self.logger.info(e.args[0])
                    return True

            else:
                self.logger.info(
                    "ERROR: invalid method for WSE interpolation: '%s'." %
                    self.method)
                return True

            try:
                self.logger.info("Saving WLE raster to: %s" %
                                 os.path.join(self.out_dir, self.out_wle))
                ras_wle_dem.save(os.path.join(self.out_dir, self.out_wle))
                self.logger.info("OK")
                self.save_info_file(os.path.join(self.out_dir, self.out_wle))
                """ ***
                if self.method == "Kriging":
                    self.logger.info("Saving WLE Kriging variance raster (%s) ..." % os.path.join(self.out_dir, self.out_wle_var))
                    ras_wle_var.save(os.path.join(self.out_dir, self.out_wle_var))
                    self.logger.info("OK")
                """
                arcpy.CheckInExtension('Spatial')
            except arcpy.ExecuteError:
                self.logger.info(arcpy.AddError(arcpy.GetMessages(2)))
                return True
            except Exception as e:
                self.logger.info(arcpy.GetMessages(2))
                self.logger.info(e.args[0])
                return True

        except arcpy.ExecuteError:
            self.logger.info("ExecuteERROR: (arcpy).")
            self.logger.info(arcpy.GetMessages(2))
            return True
        except Exception as e:
            self.logger.info("ExceptionERROR: (arcpy).")
            self.logger.info(e.args[0])
            return True

        self.clean_up()

        # return Boolean False if successful.
        return False
Пример #5
0
def EBK_ga(out_file, zField):
    normaltime = time.strftime('%Y-%m-%d %H:%M:%S',
                               time.localtime(time.time()))
    arcpy.AddMessage(normaltime + ":" + out_file + "正在进行经验贝叶斯克里金插值...")
    outTableName = arcpy.ValidateTableName(
        os.path.basename(out_file.strip(".xls")), out_gdb)
    print(outTableName)
    outTable = os.path.join(out_gdb, outTableName)
    print('Converting sheet1 to {}'.format(outTable))
    # Perform the conversion
    dbfTable = os.path.join(outdBASEPath, outTableName + '.dbf')
    try:
        arcpy.ExcelToTable_conversion(out_file, outTable,
                                      "Sheet1")  # Excel to Table
        arcpy.TableToDBASE_conversion(outTable, outdBASEPath)  #Table to dbf
    except Exception as err:
        print("{} is existing".format(dbfTable))
        arcpy.AddMessage(err.message)
    # dbaseTableName = filename.strip(".xls")
    # print (dbaseTableName)
    # outTable = os.path.join(outgdb, dbaseTableName)
    # print (outTable)
    # arcpy.ExcelToTable_conversion(xlsTable, outTable, "Sheet1")

    x_coords = 'Long'  #list(date[u'Long'].head())
    y_coords = 'Lat'  #list(date[u'Lat'].values)
    outLayerName = outTableName + '.lyr'
    outLayer = os.path.join(outLayerPath, outLayerName)
    spRef = "Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"
    try:
        arcpy.MakeXYEventLayer_management(dbfTable, x_coords, y_coords,
                                          outLayerName, spRef)
    except Exception as err:
        arcpy.AddMessage("MakeXYEventLayer_management: " + outLayerName +
                         " created Failed")
        arcpy.AddMessage(err.message)
    try:
        arcpy.SaveToLayerFile_management(outLayerName, outLayer)
    except Exception as err:
        arcpy.AddMessage("SaveToLayerFile_management: " + outLayer +
                         " created Failed")
        arcpy.AddMessage(err.message)
    try:
        #lyr to shp
        arcpy.FeatureClassToShapefile_conversion(outLayer, outShpPath)
    except Exception as err:
        arcpy.AddMessage("FeatureClassToShapefile_conversion: " + outShpPath +
                         " created Failed")
        arcpy.AddMessage(err.message)
    # Set local variables
    inPointFeatures = os.path.join(outShpPath, outTableName + '_lyr.shp')
    Output_geostatistical_layer = ""
    outRasNa = outTableName + '.tif'
    nt = time.strftime('%Y%m%d', time.localtime(time.time()))
    dt = time.strftime('%m%d%H', time.localtime(time.time()))
    outFilePath = "F:\\xiaju\\" + nt + "\\" + houtime + "\\" + zField + "\\" + "tif"
    try:
        os.makedirs(outFilePath)
    except:
        print("")
    outRaster = os.path.join(outFilePath, outRasNa)
    cellSize = 0.001
    transformation = "NONE"
    maxLocalPoints = 50
    overlapFactor = 0.5
    numberSemivariograms = 100
    # Set variables for search neighborhood
    radius = 0.3
    smooth = 0.1
    try:
        #lyr to shp
        searchNeighbourhood = arcpy.SearchNeighborhoodSmoothCircular(
            radius, smooth)
    except Exception as err:
        arcpy.AddMessage("SearchNeighborhoodSmoothCircular: " + " Failed")
        arcpy.AddMessage(err.message)
    outputType = "PREDICTION"
    quantileValue = ""
    thresholdType = ""
    probabilityThreshold = ""
    semivariogram = "POWER"
    tempEnvironment0 = arcpy.env.extent
    arcpy.env.extent = Extent
    # Execute EmpiricalBayesianKriging
    try:
        arcpy.EmpiricalBayesianKriging_ga(
            inPointFeatures, zField, Output_geostatistical_layer, outRaster,
            cellSize, transformation, maxLocalPoints, overlapFactor,
            numberSemivariograms, searchNeighbourhood, outputType,
            quantileValue, thresholdType, probabilityThreshold)
        print('Converting {} to {}'.format(inPointFeatures, outRasNa))
        arcpy.AddMessage(normaltime + ":" + "经验贝叶斯克里金插值完成")
    except Exception as err:
        arcpy.AddMessage("EmpiricalBayesianKriging_ga: " + " Failed")
        arcpy.AddMessage(err.message)
    arcpy.env.extent = tempEnvironment0
Пример #6
0
# Set local variables
inPointFeatures = "ca_ozone_pts.shp"
zField = "ozone"
outLayer = "outEBK"
outRaster = "C:/gapyexamples/output/ebkout"
cellSize = 10000.0
transformation = "EMPIRICAL"
maxLocalPoints = 50
overlapFactor = 0.5
numberSemivariograms = 100
# Set variables for search neighborhood
radius = 300000
smooth = 0.6
searchNeighbourhood = arcpy.SearchNeighborhoodSmoothCircular(radius, smooth)
outputType = "PREDICTION"
quantileValue = ""
thresholdType = ""
probabilityThreshold = ""
semivariogram = "K_BESSEL"
# Check out the ArcGIS Geostatistical Analyst extension license
arcpy.CheckOutExtension("GeoStats")

# Execute EmpiricalBayesianKriging
arcpy.EmpiricalBayesianKriging_ga(inPointFeatures, zField, outLayer, outRaster,
                                  cellSize, transformation, maxLocalPoints,
                                  overlapFactor, numberSemivariograms,
                                  searchNeighbourhood, outputType,
                                  quantileValue, thresholdType,
                                  probabilityThreshold, semivariogram)
Пример #7
0
    # outputType = Either PREDICTION or PREDICTION_STANDARD_ERROR as set in the EBK function

    quantileValue = ""
    thresholdType = ""
    probabilityThreshold = ""

    # Execute Empirical Bayesian Kriging for Prediction Surfaces

    print "\n\t 1 of 1: Starting EBK..."

    for s in zField:
        print "\t\t Creating the prediction surface for %s..." % s
        arcpy.EmpiricalBayesianKriging_ga(
            inPointFeatures, s, outLayer, outRaster + "EBK" + s, cellSize,
            transformation, maxLocalPoints, overlapFactor,
            numberSemivariograms, searchNeighborhood, "PREDICTION",
            quantileValue, thresholdType, probabilityThreshold)

        print "\t\t\t Finished the prediction surface for %s!" % s

        # Execute Empirical Bayesian Kriging for Standard Error Surfaces

        print "\n\t\t Creating the standard error surface for %s..." % s

        arcpy.EmpiricalBayesianKriging_ga(
            inPointFeatures, s, outLayer, outRaster + "EBK" + s + "_SE",
            cellSize, transformation, maxLocalPoints, overlapFactor,
            numberSemivariograms, searchNeighborhood,
            "PREDICTION_STANDARD_ERROR", quantileValue, thresholdType,
            probabilityThreshold)
Пример #8
0
    # Set contour filling parameters
    out_vector = 'kriged_vectors/' + img + '/' + img + '_krig_vector.shp'
    class_type = 'QUANTILE'
    contour_type = 'FILLED_CONTOUR'
    classes_count = 15

    # Set cv parameters
    out_cv_shp = 'kriged_vectors/' + img + '/' + img + '_krig_cv.shp'
    in_cv_dbf = 'kriged_vectors/' + img + '/' + img + '_krig_cv.dbf'
    out_cv_csv_name = img + '_krig_cv.csv'

    # Empirical Bayesian Kriging
    ebayes_krig = arcpy.EmpiricalBayesianKriging_ga(
        in_features=in_features,
        z_field=field,
        out_ga_layer='ga_layer',
        cell_size=cellsize,
        transformation_type=transformation,
        output_type=output_type,
        semivariogram_model_type=semivariogram)

    arcpy.SaveToLayerFile_management(in_layer=ebayes_krig,
                                     out_layer=out_ga_layer,
                                     is_relative_path='ABSOLUTE')

    # Cross-validate kriged raster and export prediction vs. actual as table
    cv = arcpy.CrossValidation_ga(in_geostat_layer=out_ga_layer,
                                  out_point_feature_class=out_cv_shp)
    arcpy.TableToTable_conversion(in_rows=in_cv_dbf,
                                  out_path=str(raster_path),
                                  out_name=out_cv_csv_name)
    arcpy.Delete_management(out_cv_shp)