POST
|
Hello Joshua, Have you found a solution to draw the plots in ArcGIS? I just want to share with you that it is possible do draw the plots using a python script, and I just figure out a way by which one can accomplish that. That is part of something bigger that I am working on for drone imagery analysis, and I am not ready to share it yet. I am working on write that up. Hopefully we can get that published soon. Best, Paulo
... View more
10-18-2018
09:14 PM
|
0
|
0
|
1134
|
POST
|
Charlie, I think that the issue that I am still experiencing is due some hardware problem on my desktop. Until this day I get the same error when trying to save a raster after algebra calculation. I use the same code on different laptops and it has worked just fine. Best, Paulo
... View more
05-31-2018
07:15 AM
|
0
|
1
|
338
|
POST
|
So, the code below has been working for me today on the same desktop that I am still having issues with the first code that I posted here. I ran the code below five times tonight, using three different rasters, ranging in size from around 0.5 to 1.8 GB, and it worked every time. I hope it will remain like that. import arcpy, string, os, errno
from arcpy import env
from arcpy.sa import*
from datetime import datetime
arcpy.CheckOutExtension("spatial")
arcpy.env.overwriteOutput = True
start_time = datetime.now()
imagery = r'E:\UAV2017\MW70ftP10Python\MW70ft05122017.tif'
#Get path information to create Results folder, where NDVI and NDRE will be saved
folder = os.path.dirname(imagery)
print folder
base1 = os.path.basename(imagery)
base = os.path.splitext(base1)[0]
print base
#Saving name for Excess Green mosaic - using basename from the original imagery
exgr_filename = base + "_ExGr.tif"
nred_filename = base + "_Nred.tif"
ngreen_filename = base + "_Ngreen.tif"
nblue_filename = base + "_Nblue.tif"
#Getting the ratser's bands and saving them as individual rasters
desc = arcpy.Describe(imagery)
for band in desc.children:
bandName = band.name
band_path = os.path.join(imagery, bandName)
dest_path = os.path.join(folder, bandName + '.tif')
arcpy.CopyRaster_management(band_path, dest_path, "", "", "", "NONE", "NONE", "")
print "Bands saved as rasters"
#Set workspace to the location were the results will be saved
arcpy.env.workspace = folder
band_1 = "Band_1.tif"
band_2 = "Band_2.tif"
band_3 = "Band_3.tif"
float_band1 = Float(band_1)
float_band2 = Float(band_2)
float_band3 = Float(band_3)
#Excess Green calculation - (2*Ngreen-Nred-Nblue)
nred = float_band1 / (float_band1 + float_band2 + float_band3)
print "Nred calculated"
ngreen = float_band2 / (float_band1 + float_band2 + float_band3)
print "Ngreen calculated"
nblue = float_band3 / (float_band1 + float_band2 + float_band3)
print "Nblue calculated"
exgr = (2 * ngreen) - nred - nblue
exgr.save(exgr_filename)
print "Excess Green successfully calculated"
#Build pyramids and calculate statistics for each raster in env.worspace folder
arcpy.BuildPyramids_management(exgr_filename)
arcpy.BuildPyramids_management(exgr_filename)
print "Pyramids and Statistics successful"
end_time = datetime.now()
print('Duration: {}'.format(end_time - start_time))
... View more
02-21-2018
08:47 PM
|
0
|
0
|
766
|
POST
|
Dan, I am not sure what code version you are looking at and founding line 25 as print bands I am referring to the code posted on Feb 12, 2018 8:26 AM. On that version line 25 is as follow: exgr_filename = os.path.join(Results, base + "_ExGr.tif") and line 33 is this one arcpy.env.workspace = Results So, would make any difference on the code performance (sorry for the lack of a better term) if line 25 gets moved down to line 34 on that code? Thanks again for your help.
... View more
02-20-2018
04:18 PM
|
0
|
1
|
766
|
POST
|
@ Dan Patterson, @ Xander Bakker On my original code, posted above, does it matter the position of the item on line 25? Would the code work any different if it gets move to line 34? I am asking that because for the first time on my laptop I got the same error message that I am getting on my desktop. I was playing around with the code and for 3 times I got the same error message. Then, I moved the variable correspondent to exgr_filename (ndvi_filename on this case) to below the line that sets the workspace (for the code above that would be below line 33). Once I did that the code ran just fine. I am not sure it makes any difference code wise or it was a random luck moment. Thanks.
... View more
02-20-2018
03:22 PM
|
0
|
3
|
766
|
POST
|
I have been playing around with the original code posted above tonight. I try to run it from PythonWin and from the Python window within ArcGIS. It worked once or twice using PythonWin and most of the times from within ArcGIS, but for the first time I got the same error message when running it within ArcGIS. I was able to compile a different code to try to accomplish the same thing than the original one. You can find the code below. It works most of the time from PythonWin, but it does not yield the same results than the original code (see image below). I ran both versions of the code from within ArcGIS and compare the results (just min and max showing on the figure below) with one raster calculated step by step using the raster calculator in ArcGIS. Could someone help me to understand why the two codes yield different results? Thanks. import arcpy, string, os, errno
from arcpy import env
from arcpy.sa import*
from datetime import datetime
arcpy.CheckOutExtension("spatial")
arcpy.env.overwriteOutput = True
start_time = datetime.now()
imagery = r'E:\UAV2017\PythonField8ExGr\Field8_90ft_08012017.tif'
#Get path information to create Results folder, where NDVI and NDRE will be saved
path = os.path.dirname(imagery)
print path
base1 = os.path.basename(imagery)
base = os.path.splitext(base1)[0]
print base
#Saving name for Excess Green mosaic - using basename from the original imagery
exgr_filename = base + "_ExGr.tif"
Nred = "Nred.tif"
Nblue = "Nblue.tif"
Ngreen = "Ngreen.tif"
#Set workspace to multiband raster, then list rasters to get band names
arcpy.env.workspace = imagery
bands = [Raster(os.path.join(imagery, b)) for b in arcpy.ListRasters()]
print bands
Red = imagery + "\Band_1"
print Red
Green = imagery + "\Band_2"
print Green
Blue = imagery + "\Band_3"
print Blue
red_out = "Red.tif"
green_out = "Green.tif"
blue_out = "Blue.tif"
arcpy.CopyRaster_management(Red, red_out)
arcpy.CopyRaster_management(Green, green_out)
arcpy.CopyRaster_management(Blue, blue_out)
print "Rasters successfully copied"
#Set workspace to the location were the results will be saved
arcpy.env.workspace = path
nred_num = arcpy.sa.Float(Raster(red_out))
Denom = arcpy.sa.Float(Raster(red_out) + Raster(green_out)+ Raster(blue_out))
Nred_eq = arcpy.sa.Divide(nred_num, Denom)
Nred_eq.save(Nred)
print "Nred saved"
ngreen_num = arcpy.sa.Float(Raster(green_out))
Ngreen_eq = arcpy.sa.Divide(ngreen_num, Denom)
Ngreen_eq.save(Ngreen)
print "Ngreen saved"
nblue_num = arcpy.sa.Float(Raster(blue_out))
Nblue_eq = arcpy.sa.Divide(nblue_num, Denom)
Nblue_eq.save(Nblue)
print "NBlue saved"
Ngreen_x2 = arcpy.sa.Times(Ngreen, 2)
Nred_minus_Nblue = arcpy.sa.Minus(Nred, Nblue)
ExGr_eq = arcpy.sa.Minus(Ngreen_x2, Nred_minus_Nblue)
ExGr_eq.save(exgr_filename)
print "Excess Green calculation successful"
#Build pyramids and calculate statistics for each raster in env.worspace folder
arcpy.BuildPyramids_management(exgr_filename)
arcpy.BuildPyramids_management(exgr_filename)
print "Pyramids and Statistics successful"
end_time = datetime.now()
print('Duration: {}'.format(end_time - start_time))
... View more
02-16-2018
09:59 PM
|
0
|
0
|
766
|
POST
|
I am not sure if understand your comment Dan. I still have Python2.7 with the new ArcGIS 10.6 installation.
... View more
02-16-2018
09:30 PM
|
0
|
1
|
766
|
POST
|
I got ArcGIS 10.6 installed on my computer this morning. I tried to run my original code (above) a couple times and I get the same error message than I was getting with ArcGIS 10.5. One thing that I noticed when I get this error message is that it happens really fast (within few seconds). It seems like it is not performing the calculations on lines 35, 36, and 37. If I add print statements under those algebra functions, they are printed within those few seconds before I get the error message. When I run the code on my laptop computer it takes much longer to print each of those messages (like it is taking the time to actually perform the calculation). Any advise or suggestion? Thanks, Paulo.
... View more
02-16-2018
01:59 PM
|
0
|
3
|
636
|
POST
|
Dan, Here is a smaller imagery https://filetransfer.ndsu.edu/link/n5DOUyXirQIdAf5nudoene if you still want to play around with it. It is around 350MB in size. Best, Paulo
... View more
02-14-2018
07:35 PM
|
0
|
1
|
636
|
POST
|
Dear Dan and Xander, Thanks again for your help with my code issues. I have ran Xander's code with using a at least 2 different rasters and it works just fine (Surface Pro 3 tablet). I am still working on my original code since it saves the ExGr raster on the same folder than the RGB raster, and I have another piece of code that runs some analysis on those two rasters. So, it is easier for me if both of them are located on the same folder, I already have the other piece of code. I ran the original code with 6 different rasters using my Surface Pro 3 (Windows 10 and ArcGIS 10.4) and it worked every time, but I can not run neither code version on my desktop computer. I talked with our IT folks and they are getting me ArcGIS 10.6 and ArcGIS Pro. I will try to run the codes again on my desktop with the new software and I will let you guys know how that goes. Best.
... View more
02-14-2018
07:29 PM
|
1
|
0
|
636
|
POST
|
Dan, I cleaned some space on my disk last Friday. I have quite a bit of empty space on my C hard drive (around 400 GB) and over 1 TB on my E drive. So, I am not sure that space would be an issue on this case. Thanks
... View more
02-12-2018
02:57 PM
|
0
|
0
|
636
|
POST
|
Xander, Thanks for the code. I ran it twice on my desktop and it gave me the same error message. Everything works just fine until it reach the point to save the exgr raster. I ran it on both my laptop computer and my Surface Pro 3 and it ran just fine. I am guessing that there is some kind of issue with my desktop computer. Do you thing that reinstalling ArcGIS 10.5 would help with my issues? Thanks again.
... View more
02-12-2018
02:52 PM
|
0
|
1
|
636
|
POST
|
Hi Xander, I ran the code twice previously using the PythonWin and it worked. I just try to run the code again, the same way, after restarting my computer and gives me a message that the “bands” is not defined, which is exactly what you are saying. Thanks.
... View more
02-12-2018
12:49 PM
|
1
|
3
|
665
|
POST
|
The code is posted above, with the results and the ArcGIS screenshot.
... View more
02-12-2018
11:58 AM
|
0
|
9
|
665
|
POST
|
Dear Xander and Dan, I made a small change on the code an it is working now. Maybe you guys can help me understand why. I just basically comment out lines 31, 32, and 33. I ran the code using a shorter name version (without the base) and a long one (with the base as part of the name), and both of them worked. The only other change was line 36, where the env.workspace is set to path (line 13). You can see the results bellow and the raster image in ArcGIS. import arcpy, string, os, errno
from arcpy import env
from arcpy.sa import*
from datetime import datetime
arcpy.CheckOutExtension("spatial")
arcpy.env.overwriteOutput = True
start_time = datetime.now()
imagery = r'E:\UAV2017\PythonField8ExGr\Field8_90ft_08012017.tif'
#Get path information to create Results folder, where NDVI and NDRE will be saved
path = os.path.dirname(imagery)
print path
base1 = os.path.basename(imagery)
base = os.path.splitext(base1)[0]
print base
folder = "Results2"
Results2 = os.path.join(path,folder)
if not os.path.exists(Results2):
os.makedirs(Results2)
print "Results folder successfully created"
#Saving name for Excess Green mosaic - using basename from the original imagery
exgr_filename = os.path.join(Results, base + "_ExGr.tif")
ngreen_filename = os.path.join(Results, base + "Ngreen.tif")
nred_filename = os.path.join(Results, base + "Nred.tif")
nblue_filename = os.path.join(Results, base + "Nblue.tif")
#Set workspace to multiband raster, then list rasters to get band names
##arcpy.env.workspace = imagery
##bands = [Raster(os.path.join(imagery, b)) for b in arcpy.ListRasters()]
##print bands
#Set workspace to the location were the results will be saved
arcpy.env.workspace = path
#Excess Green calculation - (2*Ngreen-Nred-Nblue)
Ngreen = (Float(bands[1])) / (Float(bands[0]) + bands[1] + bands[2])
Ngreen.save(ngreen_filename)
print "Ngreen succesfully calculated"
Nred = (Float(bands[0])) / (Float(bands[0]) + bands[1] + bands[2])
Nred.save(nred_filename)
print "Nred succesfully calculated"
Nblue = (Float(bands[2])) / (Float(bands[0]) + bands[1] + bands[2])
Nblue.save(nblue_filename)
print "Nblue succesfully calculated"
exgr = ((2 * Ngreen) - Nred - Nblue)
exgr.save(exgr_filename)
print "Excess Green successfully calculated"
#Build pyramids and calculate statistics for each raster in env.worspace folder
arcpy.BuildPyramids_management(exgr_filename)
arcpy.CalculateStatistics_management(exgr_filename)
print "Pyramids and Statistics successful"
end_time = datetime.now()
print('Duration: {}'.format(end_time - start_time)) E:\UAV2017\PythonField8ExGr
Field8_90ft_08012017
Results folder successfully created
Ngreen succesfully calculated
Nred succesfully calculated
Nblue succesfully calculated
Excess Green successfully calculated
Pyramids and Statistics successful
Duration: 0:02:50.354000
... View more
02-12-2018
11:44 AM
|
0
|
11
|
665
|
Title | Kudos | Posted |
---|---|---|
1 | 02-14-2018 07:29 PM | |
1 | 02-12-2018 12:49 PM | |
1 | 02-12-2018 08:32 AM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:24 AM
|