Skip to content

Commit

Permalink
included the r scripts
Browse files Browse the repository at this point in the history
included the r scripts in github to have a better versioning control.
  • Loading branch information
lgperez committed Jul 17, 2018
1 parent 3ebe959 commit 4b26085
Show file tree
Hide file tree
Showing 307 changed files with 18,615 additions and 0 deletions.
89 changes: 89 additions & 0 deletions ArcGIS/##Arcpy on Python Console ArcMAP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
##Arcpy on Python Console ArcMAP
#################### CHANGE SYMBOLOGY###############
import arcpy, sys

try:
symbologyLayer = arcpy.mapping.Layer(sys.argv[1])
except:
arcpy.AddError("Input parameters could not be resolved")
sys.exit(-1)

arcpy.AddMessage("Source Layer is %s" % symbologyLayer.symbologyType)

# Exit if the source layer is broken
if symbologyLayer.isBroken:
arcpy.AddError("Source layer is broken")
sys.exit(-2)

# Exit if the source layer is not a raster layer
if not symbologyLayer.isRasterLayer:
arcpy.AddError("Source layer is not a raster layer")
sys.exit(-3)
mxd = arcpy.mapping.MapDocument("Current") # This MXD

#df = arcpy.mapping.ListDataFrames(mxd,"Georgia")[0] # the first data frame called Georgia
df = arcpy.mapping.ListDataFrames(mxd)[0] # Just the first data frame in the MXD
rasters = arcpy.mapping.ListLayers(mxd,"*",df) # all the layers

for ThisLayer in rasters:
arcpy.AddMessage( "Working on " + ThisLayer.name)
if not ThisLayer.isBroken: # only try to work with layers that aren't broken
arcpy.AddMessage( "-not broken")
if not ThisLayer.name.upper() == symbologyLayer.name.upper():
arcpy.AddMessage( "-not the source layer")
# not the source layer
if ThisLayer.isRasterLayer:
arcpy.AddMessage( "-is a raster layer")
# only applies to raster layers
arcpy.CalculateStatistics_management(ThisLayer.dataSource)
arcpy.AddMessage( "--Statistics calculated")
arcpy.AddMessage( "--Raster symbology is %s" % ThisLayer.symbologyType)
arcpy.ApplySymbologyFromLayer_management(ThisLayer,symbologyLayer)
arcpy.AddMessage( "--Symbology Applied")

#mxd.save() # I'm not saving, uncomment this to save
del mxd


############################ TURN ON AND OFF##########################

l1 = arcpy.mapping.Layer(r'h160303\h160303bgsort.bsq') # Get layer by its name (belongs to group)
#l1.longName # This will give the name including the group

#A list of layer names that you want to be turned off.
names = [x,y,z,etc]

mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(, mxd, "Layers")[0] #Get the 1st dataframe called "Layers"
layers = arcpy.mapping.ListLayers(mxd, "*", df)

for layer in layers:
if layer.name in names:
layer.visible = False

arcpy.RefreshTOC()
arcpy.RefreshActiveView()

###################turn off layer in group layer h160303?
#lyr.isGroupLayer == 1
mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] #Get the 1st dataframe called "Layers"
groupLayer = arcpy.mapping.Layer(r'h160303') # fixed the name of the group layer
for i in arcpy.mapping.ListLayers(groupLayer): print (i) ## Print layers in group layer, INCLUDES THE layer group itself
for i in arcpy.mapping.ListLayers(groupLayer): i.visible = True ## Turn Off layers in group layer
>>> arcpy.RefreshTOC() # Refresh Layers Panel

## Calsulate statistics and apply symbology from layer on all layer of group layer

layerRef = arcpy.mapping.Layer("h160303bgsort.bsq") #Layer to use as symbology, hardcoded
groupLayer = arcpy.mapping.Layer(r'h160303') # fixed the name of the group layer
for i in arcpy.mapping.ListLayers(groupLayer):
if not i.isGroupLayer:
if not i.name.upper() == layerRef.name.upper(): #If its not the reference layer
if i.isRasterLayer: #apply only to rasters
arcpy.CalculateStatistics_management(i.dataSource,"1","1","0")
arcpy.ApplySymbologyFromLayer_management(i,layerRef)
i.visible = False #Uncheck visibility
arcpy.RefreshTOC() #Apply the layer state update: visibility
arcpy.RefreshActiveView()

193 changes: 193 additions & 0 deletions ArcGIS/Batch_zonal_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# joinZonalStats_hyper_BW_pruebas.py
# Created on: 2014-06-07 11:06:16.00000
# (generated by ArcGIS/ModelBuilder)
# Usage: joinZonalStats_hyper_BW_pruebas <Buffer> <Stats_table> <Grid> <Zone_field_and_join_Field> <Field_Name_Calculate_field> <Expression_Calculate_field> <Expression> <band> <Raster_set>
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import os
import arcpy

try:
# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

#workspace

bwPath = "D:\\AB2013-2014\\Bread_wheat"
bwVectoresPath = bwPath + "\\" + "vectores"
bwDataPath = bwPath + "\\" + "data"
bwGdbPath = bwPath + "\\" + "AB_BreadWheat.gdb"
tempGdb = "C:\\vuelos\\temp.gdb"
arcpy.env.workspace = bwPath

#List of band names
bandNames = ["392.03027","393.87228","395.71426","397.55627","399.39830","401.24026","403.08228","404.92430","406.76627","408.60828","410.45030","412.29227","414.13428","415.97630","417.81827","419.66028","421.50230","423.34427","425.18628","427.02830","428.87027","430.71228","432.55430","434.39627","436.23828","438.08030","439.92227","441.76428","443.60630","445.44827","447.29028","449.13230","450.97427","452.81628","454.65826","456.50027","458.34230","460.18427","462.02628","463.86830","465.71027","467.55228","469.39430","471.23627","473.07828","474.92030","476.76227","478.60428","480.44630","482.28827","484.13028","485.97230","487.81427","489.65628","491.49830","493.34027","495.18228","497.02430","498.86627","500.70828","502.55030","504.39227","506.23428","508.07626","509.91827","511.76028","513.60230","515.44430","517.28625","519.12830","520.97030","522.81226","524.65430","526.49630","528.33826","530.18030","532.02230","533.86426","535.70630","537.54830","539.39026","541.23230","543.07430","544.91626","546.75830","548.60030","550.44226","552.28430","554.12630","555.96826","557.81030","559.65230","561.49426","563.33630","565.17830","567.02026","568.86230","570.70430","572.54626","574.38830","576.23030","578.07227","579.91430","581.75630","583.59827","585.44030","587.28230","589.12427","590.96630","592.80830","594.65027","596.49225","598.33430","600.17630","602.01825","603.86030","605.70230","607.54425","609.38630","611.22830","613.07025","614.91230","616.75430","618.59625","620.43830","622.28030","624.12225","625.96430","627.80630","629.64825","631.49030","633.33230","635.17426","637.01630","638.85830","640.70026","642.54230","644.38430","646.22626","648.06830","649.91030","651.75226","653.59430","655.43630","657.27826","659.12030","660.96230","662.80426","664.64630","666.48830","668.33026","670.17230","672.01430","673.85626","675.69830","677.54030","679.38226","681.22430","683.06630","684.90826","686.75030","688.59230","690.43427","692.27630","694.11830","695.96027","697.80230","699.64430","701.48627","703.32830","705.17030","707.01227","708.85430","710.69630","712.53827","714.38025","716.22230","718.06430","719.90625","721.74830","723.59030","725.43225","727.27430","729.11630","730.95825","732.80030","734.64230","736.48425","738.32630","740.16830","742.01025","743.85230","745.69430","747.53625","749.37830","751.22030","753.06226","754.90430","756.74630","758.58826","760.43030","762.27230","764.11426","765.95630","767.79830","769.64026","771.48230","773.32430","775.16626","777.00830","778.85030","780.69226","782.53430","784.37630","786.21826","788.06030","789.90230","791.74426","793.58630","795.42830","797.27026","799.11230","800.95430","802.79626","804.63830","806.48030","808.32227","810.16430","812.00630","813.84827","815.69030","817.53230","819.37427","821.21630","823.05830","824.90027","826.74225","828.58430","830.42630","832.26825","834.11030","835.95230","837.79425","839.63630","841.47830","843.32025","845.16230","847.00430","848.84625","850.68830"]
#Short list of band names for test
#bandNames = ["392.03027","393.87228"] #Testing purpose


# Load required toolboxes
print "Start!"
#Type: Feature layer
bf_Lx_1 = arcpy.GetParameterAsText(0)
arcpy.AddMessage("read b1 " + bf_Lx_1)
print bf_Lx_1


#Type: Feature layer
# Script arguments
bf_Lx_2 = arcpy.GetParameterAsText(1)
#bf_Lx_2 = bwVectoresPath + "\\" + "EYTBWMEL\\H\\H140117_EYTBWMEL_bf_L3.shp" # provide a default value if unspecified
arcpy.AddMessage("read b2 " + bf_Lx_2)
print bf_Lx_2

#Type: Feature layer
# Script arguments
bf_Lx_3 = arcpy.GetParameterAsText(2)
arcpy.AddMessage("read b3 " + bf_Lx_2)
print bf_Lx_3

#type: Feature layer
Grid = arcpy.GetParameterAsText(3)
#gridName = "H140117_BW"
#Grid = bwGdbPath + "\\" + gridName
arcpy.AddMessage("read " + Grid)
desc = arcpy.Describe(Grid)
gPath = desc.path
gridSource = str(gPath) + "\\" + Grid
arcpy.AddMessage("Grid source: " + gridSource)
gridName = desc.baseName
arcpy.AddMessage("Grid basename: " + gridName)

print Grid

#type: Raster layer
Raster = arcpy.GetParameterAsText(4)
#Raster = "E:\\140117\\140117H\\ortho\\140117_3_810.bsq" # provide a default value if unspecified
#Raster = "c:\\vuelos\\140117_3_810.bsq"
arcpy.AddMessage("read " + Raster)
desc = arcpy.Describe(Raster)
rPath = desc.path
Raster = str(rPath) + "\\" + desc.baseName+ "." +desc.extension
arcpy.AddMessage("Raster source: " + Raster)
print Raster

#Folder to store the tables with the bands
#type: Geodatabase
#tablesPath = arcpy.GetParameterAsText(5)
tablesPath = tempGdb
print tablesPath

# Local variables:
arcpy.AddMessage("Local variables")
print "Local variables"
Zone_field_and_join_Field = "Name"
gridLayer = "gridLayer"
#Line
line = bf_Lx_1[-2:]


## Make a layer
print "Make layer"
arcpy.MakeFeatureLayer_management(Grid, "gridLayer")

#Where to store the merge feature
print "Where to store the merge feature"
L_merge = tempGdb + "\\l_merge"
arcpy.AddMessage("merge path: " + L_merge)
if arcpy.Exists(L_merge):
arcpy.Delete_management(L_merge)

#List of features to merge
mergeList = ""

# Process: Merge
arcpy.AddMessage("Merging")
print "merging..."
if bf_Lx_1 != '#':
mergeList += bf_Lx_1
if bf_Lx_2 != '#':
mergeList += ";"+bf_Lx_2
if bf_Lx_3 != '#':
mergeList += ";"+bf_Lx_3
arcpy.AddMessage("merge List: " + str(mergeList))
m = arcpy.Merge_management(mergeList, L_merge)
arcpy.AddMessage(m)

i=1
for b in bandNames:
band = Raster + "\\" + b
bandField = "B" + str(i)
print bandField
#Set table name
tableName = gridName + "_" + line + "_" + bandField #+ ".dbf"
L_Stats_table = tablesPath + "\\" + tableName
print L_Stats_table
Field_Name_Calculate_field = gridName + "." + bandField
print Field_Name_Calculate_field
Expression_Select_layer = tableName+".Mean IS NOT NULL"
# Use this if the stats were saved to a dbf file "\""+tableName+".Mean\" IS NOT NULL"
# Use this if the stats were saved to a table "\""+tableName+":Mean\" IS NOT NULL"
# Use this if the stats were saved to a database table tableName+".Mean IS NOT NULL"
print Expression_Select_layer
Expression_Calculate_field = "round(!"+tableName+".Mean!,0)"
#the "!"+tableName+":Mean!" notation (:)is used when the stats are saved to a table
#the "!"+tableName+".Mean!" notation (.)is used when the stats are saved to a dbf file
print Expression_Calculate_field



# Process: Zonal Statistics as Table
print "Zonal statistics..."
zs = arcpy.gp.ZonalStatisticsAsTable_sa(L_merge, Zone_field_and_join_Field, band, L_Stats_table, "DATA", "MEAN")
arcpy.AddMessage("stats table: " + str(zs))

## # Process: Join Field
## print "Joining Field..."
## arcpy.AddMessage("Joining Field...")
## arcpy.JoinField_management(gridLayer, Zone_field_and_join_Field, L_Stats_table, Zone_field_and_join_Field, "MEAN")


# Process: Add Join
print "Joining..."
arcpy.AddMessage("grid..."+ gridLayer)
arcpy.AddMessage("field..."+Zone_field_and_join_Field)
arcpy.AddJoin_management(gridLayer, Zone_field_and_join_Field, zs, Zone_field_and_join_Field, "KEEP_ALL")

# Process: Select Layer By Attribute
if i==1:
print "Selecting Layer..."
gridLayerSelection = arcpy.SelectLayerByAttribute_management(gridLayer, "NEW_SELECTION", Expression_Select_layer)

# Process: Calculate Field
print "Calculating field"
arcpy.AddMessage("Calculating field...")
arcpy.CalculateField_management(gridLayerSelection, Field_Name_Calculate_field, Expression_Calculate_field, "PYTHON")

# Process: Remove Join
arcpy.AddMessage("remove Join")
arcpy.RemoveJoin_management(gridLayer, "")
#Erase stats table
#arcpy.Delete_management(zs)
i+=1

#Erase merge file
arcpy.AddMessage("delete merge temp")
arcpy.Delete_management(L_merge)
print "Finish"

except Exception, e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
arcpy.AddMessage("Line:" + str(tb.tb_lineno))
print "Line %i" % tb.tb_lineno
arcpy.AddMessage("Error " + e.message)
print e.message
print "OMG!"


104 changes: 104 additions & 0 deletions ArcGIS/CalculateNDVIandNiblackFromHyperIterate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# CalculateNDVIandNiblackFromHyperIterate.py
# Created on: 2015-08-31 23:45:21.00000
# (generated by ArcGIS/ModelBuilder)
# Usage: CalculateNDVIandNiblackFromHyperIterate <raster> <ndvi> <NIR_wl> <RED_wl> <niblack> <Dark_background> <Neighborhood> <truncate_charsEnd>
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy, os

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

#Method to truncate string
def getBasename(inputName, fromChar, toChar):
subsetName = inputName[fromChar:int(toChar)]
arcpy.AddMessage("Truncated name: "+subsetName)
return subsetName

def upLevel(locationpath):
#Up one level the path
folderUp = "\\".join(locationpath.split("\\")[0:-1])
folderUp.replace("\\", "\\\\")
return folderUp

def checkFolder(folderPath):
#Create folder if it doesnt exists
folderPath.replace("\\", "\\\\")
if not os.path.exists(folderPath):
os.makedirs(folderPath)

# Script arguments
raster = arcpy.GetParameterAsText(0)

truncate_charsEnd = arcpy.GetParameterAsText(1)
if truncate_charsEnd == '#' or not truncate_charsEnd:
truncate_charsEnd = "-3" # provide a default value if unspecified

# Process: Parse Path
desc = arcpy.Describe(raster)
name = desc.name
arcpy.AddMessage("name: "+name)
path = desc.path
arcpy.AddMessage("path: "+path)
folderUp = upLevel(path)
saveFolder = folderUp+"\\mask"
checkFolder(saveFolder) #Check if exists

# Process: truncateString to get basename
basename = getBasename(name,0,truncate_charsEnd)

ndvi = arcpy.GetParameterAsText(7)
if ndvi == '#' or not ndvi:
ndvi = saveFolder+"\\"+basename+"nvi.tif" # provide the automatic value unless other is indicated

NIR_wl = arcpy.GetParameterAsText(2)
if NIR_wl == '#' or not NIR_wl:
NIR_wl = "802.79626" # provide a default value if unspecified

RED_wl = arcpy.GetParameterAsText(3)
if RED_wl == '#' or not RED_wl:
RED_wl = "670.17230" # provide a default value if unspecified

niblack = arcpy.GetParameterAsText(4)
if niblack == '#' or not niblack:
niblack = saveFolder+"\\"+basename+"msk.tif" # provide the automatic value unless other is indicated

Dark_background = arcpy.GetParameterAsText(5)
if Dark_background == '#' or not Dark_background:
Dark_background = "true" # provide a default value if unspecified

Neighborhood = arcpy.GetParameterAsText(6)
if Neighborhood == '#' or not Neighborhood:
Neighborhood = "Rectangle 3 3 CELL" # provide a default value if unspecified



# Local variables:
focalSTD = "C:\Users\usuario\Documents\ArcGIS\Default.gdb\std"
focalMEAN = "C:\Users\usuario\Documents\ArcGIS\Default.gdb\mean"

#Assign names

NIR_path = raster+"\\"+NIR_wl
RED_path = raster+"\\"+RED_wl


# Process: Raster Calculator
arcpy.gp.RasterCalculator_sa("Float((Float(\""+NIR_path+"\") - Float(\""+RED_path+"\"))/(Float(\""+NIR_path+"\") + Float(\""+RED_path+"\")))", ndvi)

# Process: Focal MEAN 3x3
arcpy.gp.FocalStatistics_sa(ndvi, focalMEAN, Neighborhood, "MEAN", "DATA")

# Process: Focal STD 3x3
arcpy.gp.FocalStatistics_sa(ndvi, focalSTD, Neighborhood, "STD", "DATA")

# Process: calculate Nib
#######REEEROOR
arcpy.gp.RasterCalculator_sa("Con("+Dark_background+" == true,Con(\"Float("+ndvi+")\">(\"Float("+focalMEAN+")\" +(0.2 * \"Float("+focalSTD+")\")),1,0),Con(\"Float("+ndvi+")\">(\"Float("+focalMEAN+")\" +(Float(-0.2) * \"Float("+focalSTD+")\")),1,0))", niblack)
#Delete temporals
arcpy.Delete_management(FocalSTD)
arcpy.Delete_management(FocalMEAN)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added ArcGIS/ChooseFieldValue/Scratch/Scratch.gdb/gdb
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions ArcGIS/ChooseFieldValue/Script/DynamicValueList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Import system modules
import arcpy

# Set the parameters
InputFeatureClass = arcpy.GetParameterAsText(0)
InputField = arcpy.GetParameterAsText(1)
InputValue = arcpy.GetParameterAsText(2)

arcpy.SetParameter(3, InputValue)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added ArcGIS/ChooseFieldValue/ToolData/ToolData.gdb/gdb
Binary file not shown.
Binary file not shown.
Binary file added ArcGIS/ENVI.tbx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 4b26085

Please sign in to comment.