I am very new to GDAL library (trying it as of today actually) and I have a hard time figuring out what I'm doing wrong here. I am trying to execute the 'proximity' function from GDAL, but keep getting the ''str' object is not callable' error message.
Can anyone indicate me what I'm doing wrong.
import os
from osgeo import gdal, osr
gdal_proximity = "C:\\anaconda3\\envs\\geo_py37\\Scripts\\gdal_proximity.py"
proximityInput = gdal.Open(folderPath + os.sep + "proximity_input.tif")
outputTemplate = gdal.Open(folderPath + os.sep + "output_template.tif")
######## Raster properties based on 'outputTemplate' ################
projection = outputTemplate.GetProjection()
ncols = outputTemplate.RasterXSize
nrows = outputTemplate.RasterYSize
bandCount = outputTemplate.RasterCount
upx, xres, xskew, upy, yskew, yres = outputTemplate.GetGeoTransform()
#####################################################################
driver = gdal.GetDriverByName('Gtiff')
proximityOutput = driver.Create(folderPath + os.sep + "proximity_output.tif", ncols, nrows, bandCount, gdal.GDT_Float32)
proximityOutput.SetGeoTransform([upx, xres, xskew, upy, yskew, yres])
proximityOutputPrj = distanceRaster.SetProjection(projection)
gdal_proximity (proximityInput, proximityOutputPrj)
Traceback:
Traceback (most recent call last):
File "C:/Users/antoi/.PyCharmCE2019.3/config/scratches/Radial_Linear_Mean.py", line 22, in <module>
gdal_proximity(proximityInput, proximityOutputPrj)
TypeError: 'str' object is not callable
in your third line of code you set (overwrite) gdal_proximity to be the following string
gdal_proximity = "C:\\anaconda3\\envs\\geo_py37\\Scripts\\gdal_proximity.py"
You then try to call it as a function.
gdal_proximity (proximityInput, proximityOutputPrj)
which triggers your error because its not a function, its a string, so is not callable as a function.
Related
I am trying to use python gdal to process a grib2 file into a geotiff file based on the following example:
https://geoexamples.com/d3-raster-tools-docs/code_samples/vardah.html
based on this I have the following code:
import gdal
import osr
ds = gdal.Open(r"/home/test/gfs.t12z.sfluxgrbf000.grib2",1)
gph = ds.GetRasterBand(84).ReadAsArray()
press = ds.GetRasterBand(54).ReadAsArray() / 100
temp = ds.GetRasterBand(52).ReadAsArray()
u = ds.GetRasterBand(50).ReadAsArray()
v = ds.GetRasterBand(51).ReadAsArray()
corr_press = press * (1 - (0.0065*gph/(0.0065*gph + temp + 273.15)))**-5.257
driver = gdal.GetDriverByName('GTiff')
outRaster = driver.Create("/home/test/vardah2.tiff", ds.RasterXSize, ds.RasterYSize, 4, gdal.GDT_Float32)
outRaster.SetGeoTransform(ds.GetGeoTransform())
outband = outRaster.GetRasterBand(1)
outband.WriteArray(corr_press)
outband.SetMetadata({'name': 'press'})
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromEPSG(4326)
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outband.FlushCache()
I am attempting to do this on centos 7 and when I run the program I get the following error:
ERROR 6: The GRIB driver does not support update access to existing datasets.
Traceback (most recent call last):
File "ficky.py", line 4, in <module>
gph = ds.GetRasterBand(84).ReadAsArray()
AttributeError: 'NoneType' object has no attribute 'GetRasterBand'
How do I resolve this error to get a successful run of this script on a centos interface?
Change
ds = gdal.Open(r"/home/test/gfs.t12z.sfluxgrbf000.grib2",1)
to
ds = gdal.Open("/home/test/gfs.t12z.sfluxgrbf000.grib2")
or even better
ds = gdal.Open("/home/test/gfs.t12z.sfluxgrbf000.grib2", GA_ReadOnly)
I'm having a hard time here on processing GIS data in Python, using library ArcPy.
I've been trying to generate independent features from a feature class based on a field of the attribute table which is a unique code representing productive forest units, but I can't get it done.
I've already done this in other situations, but this time I don't know what I am missing.
Here is the code and the error I get:
# coding utf-8
import arcpy
arcpy.env.overwriteOutput = True
ws = r'D:\Projeto_VANT\SIG\proc_parc.gdb'
arcpy.env.workspace = ws
talhoes = r'copy_talhoes'
estados = ('SP', 'MG')
florestas = ('PROPRIA', 'PARCERIA')
arcpy.MakeFeatureLayer_management(talhoes,
'talhoes_layer',
""" "ESTADO" IN {} AND "FLORESTA" IN {} """.format(estados, florestas),
ws)
arcpy.FeatureClassToFeatureClass_conversion(in_features = 'talhoes_layer',
out_path = ws,
out_name = 'talhoes1')
talhoes1 = r'talhoes1'
arcpy.AddField_management(talhoes1, 'CONCAT_T', 'TEXT')
arcpy.CalculateField_management(talhoes1, 'CONCAT_T', """ [ESTADO] & "_" & [CODIGO] & "_" & [TALHAO] """, 'VB')
with arcpy.da.SearchCursor(talhoes1, ['CONCAT_T', 'AREA']) as tal_cursor:
for x in tal_cursor:
print(x[0] + " " + str(x[1])) # This print is just to check if the cursor works and it does!
arcpy.MakeFeatureLayer_management(x,
'teste',
""" CONCAT_T = '{}' """.format(str(x[0]))) # Apparently the problem is here!
arcpy.CopyFeatures_management('teste',
'Layer{}'.format(x[0]))
Here is the error:
Traceback (most recent call last):
File "D:/ArcPy_Classes/Scripts/sampling_sig.py", line 32, in <module>
""" CONCAT_T = '{}' """.format(str(x[0])))
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\management.py", line 6965, in MakeFeatureLayer
raise e
RuntimeError: Object: Error in executing tool
I think the issue is with your In feature. you will want your in feature to be talhoes1 since x is the cursor object and not a feature.
arcpy.MakeFeatureLayer_management(talhoes1,'teste',""" CONCAT_T =
'{}'""".format(str(x[0])))
What needs to be done to fix this problem?
At https://www.glowscript.org/#/user/murray.garth/folder/Public/program/Eyeballs I found a GlowScript example that i tried to use as a python3 script.
I modified the header to
#https://www.glowscript.org/#/user/murray.garth/folder/Public/program/Eyeballs
#GlowScript 2.1 VPython
from vpython import *
and changed the true/false references to uppercase.
running
python3 eyeballs.py
starts a static image
and then gives the error message:
compound event return
compound event return
compound event return
Traceback (most recent call last):
File "eyeballs.py", line 39, in <module>
world_pos = Head.compound_to_world( vRightEye.pos )
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/vpython/vpython.py", line 1553, in compound_to_world
v = v-self._origin
AttributeError: 'compound' object has no attribute '_origin'
According to https://www.glowscript.org/docs/VPythonDocs/compound.html
the syntax for compound_to_world is:
world_pos = c.compound_to_world(v)
Which seems to be o.k. to me.
The environment is macports python3
python3 --version
Python 3.7.4
I had installed vpython with
pip install vpython
pip --version
pip 18.1 from /opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)
Thanks for reporting this bug, which I'll report as an issue in the vpython repository. As you say, a workaround is to specify an origin.
This was a tough one.
There are some 1.2 million questions on python on stackoverflow.
If you search for
[python]"object has no attribute"
you get some 12066 results. So that's probably why this question didn'get much attention.
I tried out the issue with a smaller example according to https://www.glowscript.org/docs/VPythonDocs/compound.html
from vpython import *
handle = cylinder( size=vec(1,.2,.2),color=vec(0.72,0.42,0) )
head = box( size=vec(.2,.6,.2), pos=vec(1.1,0,0),color=color.gray(.6) )
hammer = compound([handle, head])
hammer.axis = vec(1,1,0)
world_pos = hammer.compound_to_world(hammer.axis)
giving the error mentioned in the question:
compound event return
Traceback (most recent call last):
File "hammer.py", line 10, in <module>
print (hammer.origin)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/vpython/vpython.py", line 1536, in origin
return self._origin
AttributeError: 'compound' object has no attribute '_origin'
The relevant source code lines of vpython.py are:
#property
def origin(self):
return self._origin
#origin.setter
def origin(self,value): # compound origin cannot be reset
if not self._constructing:
raise AttributeError('The compound "origin" attribute is read-only; change "pos" instead.')
self._origin = value
def world_to_compound(self, v):
v = v-self._pos
x_axis = self._axis.hat
y_axis = self._up.hat
z_axis = x_axis.cross(y_axis)
ox = self._size0.x/self._size.x # _size0 is the original size
oy = self._size0.y/self._size.y
oz = self._size0.z/self._size.z
return self._origin + vector(v.dot(x_axis)*ox, v.dot(y_axis)*oy, v.dot(z_axis)*oz)
def compound_to_world(self, v):
v = v-self._origin
x_axis = self._axis.hat
y_axis = self._up.hat
z_axis = x_axis.cross(y_axis)
ox = self._size.x/self._size0.x # _size0 is the original size
oy = self._size.y/self._size0.y
oz = self._size.z/self._size0.z
return self._pos + v.x*ox*x_axis + v.y*oy*y_axis + v.z*oz*z_axis
and indeed a few lines further up the constructor does not set any origin.
So adding a default origin:
class compound(standardAttributes):
compound_idx = 0 # same numbering scheme as in GlowScript
def __init__(self, objList, **args):
self._origin = vector(0,0,0)
makes the syntax error go away.
from vpython import *
handle = cylinder( size=vec(1,.2,.2), color=vec(0.72,0.42,0) )
head = box( size=vec(.2,.6,.2), pos=vec(1.1,0,0), color=color.gray(.6) )
hammer = compound([handle, head])
hammer.axis = vec(1,1,0)
print (hammer.origin)
world_pos = hammer.compound_to_world(hammer.axis)
print (world_pos)
then gives the result:
compound event return
<0, 0, 0>
<0.6, 1.41421, 0>
and the eyeballs.py code works as expected:
I do not know there to report this bug but I posted a message to the vpython-users group
I'm using gdal to read jp2 by lines, here is the code:
def open(self):
if self.ds is None:
self.ds = gdal.Open(self.file_path, gdal.GA_ReadOnly)
self.geo_transform = self.ds.GetGeoTransform()
self.rows = self.ds.RasterYSize
self.cols = self.ds.RasterXSize
def read_strip(self, y_start, read_y_size):
"""
y_start : y in projection coordinate
"""
self.open()
if not self.ds:
raise IOError("Could not open '%s'" % self.file_path)
y_off = int((y_start - self.geo_transform[3]) / self.geo_transform[5])
if y_off < 0 or y_off >= self.rows:
return None
else:
read_y_size_in_data = min(self.rows - y_off, read_y_size)
try:
read_data = self.ds.ReadAsArray(0, y_off, ysize=read_y_size_in_data)
band_data = read_data.astype(float)
self.logger.info('{}, {}, {}'.format(read_y_size, y_off, read_y_size_in_data))
return band_data
except Exception:
self.logger.exception('this file is Nonetype, file: {}'.format(self.file_path))
self.logger.info('{}, {}, {}'.format(read_y_size, y_off, read_y_size_in_data))
self.close()
Here is the error message:
Traceback (most recent call last): File "scene_reader.py", line 62, in read_strip band_data = read_data.astype(float), 'NoneType' object has no attribute 'astype'
Every time I run the code, I did not get any IOError, which means the jp2 file is opened successfully, while the ReadAsArray() got an 'Nonetype' error. by the way, I have tried to just use gdal.Open() and ReadAsArray() to read the same file by several lines in ipython, everything is ok, so, I'm pretty sure there is nothing wrong with the jp2 file itself.
so, can anyone help me?
I think you have to pick a band, even if it is a single band data set. ds.GetRasterBand(1).ReadAsArray() like that. I always refer to this GDAL API Tutorial which has simple examples.
I'm unable to run this code. When I run this it gives me an error which, from what I understand it means that it is unable to import cvxopt.base module.
If there is anyone who has encountered this error or knows the solution to this problem, please reach out.
I'm sharing the code & trace of Python interface below:
The Code:
#!/usr/bin/env python
import numpy as np
from numpy import linalg
from cvxopt import solvers
def Twin_plane_1(R,S,C1,Epsi1,regulz1):
StS = np.dot(S.T,S)
# for regularization we add identity matrix with wt. before inversion
StS = StS + regulz1*(np.identity(StS.shape[0]))
StSRt = linalg.solve(StS,R.T)
RtStSRt = np.dot(R,StSRt)
RtStSRt = (RtStSRt+(RtStSRt.T))/2
m2 = R.shape[0]
e2 = -np.ones((m2,1))
solvers.options['show_progress'] = False
vlb = np.zeros((m2,1))
vub = C1*(np.ones((m2,1)))
# x<=vub
# x>=vlb -> -x<=-vlb
# cdx<=vcd
cd = np.vstack((np.identity(m2),-np.identity(m2)))
vcd = np.vstack((vub,-vlb))
alpha = solvers.qp(matrix(RtStSRt,tc='d'),matrix(e2,tc='d'),matrix(cd,tc='d'),matrix(vcd,tc='d'))#,matrix(0.0,(1,m1)),matrix(0.0))#,None,matrix(x0))
alphasol = np.array(alpha['x'])
z = -np.dot(StSRt,alphasol)
w1 = z[:len(z)-1]
b1 = z[len(z)-1]
return [w1,b1]
The Trace:
Traceback (most recent call last): File
"C:\Users\sau\Downloads\Twin-SVM-master\Twin-SVM-master\TwinPlane1.py",
line 6, in
from cvxopt import solvers File "C:\Python35\lib\site-packages\cvxopt__init__.py", line 50, in
import cvxopt.base ImportError: DLL load failed: The specified module could not be found.
I was having the same issue...try this:
import os
os.environ['PATH'] += r';C:\\Users\\user\\AppData\\Local\\Continuum\\anaconda3\\Library\\mingw-
w64\\bin'