Quantcast
Channel: Question and Answer » gdal
Viewing all 397 articles
Browse latest View live

Using GDAL/C++ to calculate distance in meters

$
0
0

I’m using GDAL in C++, with WGS84. I want to calculate distance between two points in meters. The code snippet below tries to find the distance between 25N/75E and 25N/76E. The code, as is, returns 1.0000, which is just a Cartesian number. I need the answer in meters or feet or miles. How do I do that?

OGRSpatialReference  wgs84;
OGRPoint   point1,
           point2;

assert(wgs84.SetWellKnownGeogCS("WGS84")==OGRERR_NONE);
point1.assignSpatialReference(&wgs84);
point2.assignSpatialReference(&wgs84);
point1.setY(25.0000); point1.setX(75.0000);
printf("Distance %fn",point1.Distance(&point2));

point2.setY(25.0000); point2.setX(76.0000);
printf("Distance %fn",point1.Distance(&point2)); 

How to place an numpy array into GeoTIFF image using python-gdal

$
0
0

I am attempting to export data of varying scales into GeoTIFF images…a new method for me. The problem I’m running into is when using the GDT_Byte pixel type, data values < 0 and > 256 are not represented (they are black or white), and when I use GDT_Float32 or other pixel types, I get white, or transparent images. How can I represent a wider range of values?

This example creates an image with values of zero of less being black, and greater then 256 being white. Alternate pixel types result in transparent images.

from osgeo import gdal, osr, ogr
import numpy as np
#create a bogus grid
mag_grid = np.arange(-500, 500, .1).reshape((100, 100))
lats = np.arange(-10,90,1.)
lons = np.arange(-10,90,1.)

xres = lons[1] - lons[0]
yres = lats[1] - lats[0]
ysize = len(lats)
xsize = len(lons)
ulx = lons[0] - (xres / 2.)
uly = lats[-1] - (yres / 2.)


driver = gdal.GetDriverByName('GTiff')
ds = driver.Create('output.tif',
               xsize, ysize, 1, gdal.GDT_Byte, )
##################################
#pixel type of gdal.GDT_Float32 results in transparent image
##################################

# this assumes the projection is Geographic lat/lon WGS 84
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
ds.SetProjection(srs.ExportToWkt())
gt = [ulx, xres, 0, uly, 0, yres ]
ds.SetGeoTransform(gt)
outband=ds.GetRasterBand(1)
outband.SetStatistics(np.min(mag_grid), np.max(mag_grid), np.average(mag_grid), np.std(mag_grid))
outband.WriteArray(mag_grid)
ds = None

Does gdal_calc only support 26 input raster files at a time?

$
0
0

In gdal_calc.py, input files are specified with letters A-Z.

Here are two files, input.tif and input2.tif, being averaged into result.tif:

gdal_calc.py -A input.tif -B input2.tif --outfile=result.tif --calc="(A+B)/2"

I have more than 26 files. What can I do about this?

One possibility: keep adding up files, 25 at a time, and then divide the values by the total number of input files. I think my numbers will become too large.

It looks like the source code for gdal_calc.py could be adapted to process as many images as needed, but the script is a mammoth nested looping function called doit(), so that’s a bit scary. Should I really be hacking source code, or is there a better way?

My images are all single-band GeoTIFFs, by the way.

EDIT: After posting, I realized this question is somewhat of a duplicate of this rather incoherent one. Fortunately, the other question does have a good answer showing how to average more than 26 images together from the command line. The answer only works for a predetermined number of images, though. It would be nice to have a general solution that supports a theoretically unlimited number of images.

RuntimeError opening rasterio array in GDAL

$
0
0

I’ve opened a GeoTiff with rasterio:

import rasterio
with rasterio.open('Africa.tif') as src:
    transform = src.meta['transform']
    array = src.read(1)


array([[203, 203, 203, ...,   0,   0,   0],
       [203, 203, 203, ...,   0,   0,   0],
       [203, 203, 203, ...,   0,   0,   0],
       ..., 
       [  0,   0,   0, ...,   0,   0,   0],
       [  0,   0,   0, ...,   0,   0,   0],
       [  0,   0,   0, ...,   0,   0,   0]], dtype=uint8)

Then I want to open this array data in GDAL:

gdal.Open(array,GA_ReadOnly)

but I get the following error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-65-ac65027e8ea3> in <module>()
----> 1 gdal.Open(array,GA_ReadOnly)

RuntimeError: not a string

However, opening the original Tiff in GDAL works fine:

gdal.Open('Africa.tif')

<osgeo.gdal.Dataset; proxy of <Swig Object of type 'GDALDatasetShadow *' at 0x152e91450> >

I’m following the computations here: http://nbviewer.ipython.org/github/perrygeo/python-raster-stats/blob/master/docs/notebooks/Integrating%20with%20GeoPandas%20and%20Numpy.ipynb

but I want to do some masking of the input array before running the zonal computations on it (otherwise it’s not necessary to open the image in rasterio at all).

Here is the link to the Tiff file I’m using, in case that’s the issue:
https://drive.google.com/file/d/0B7dNXVF349Jsbm5mQUt5c3JCSDA/view?usp=sharing

GDAL Create Layer WFS

$
0
0

So I’m trying to write a program that has the ability to read and write data from a WFS (Web Feature Service) I’ve managed to pull all the data I want from the WFS. Now I’m trying to write to it and edit data. When ever I try to delete a layer, or add one I get this error respectively: ERROR 6: CreateLayer() not supported by this data source.

I’m assuming that means the WFS is set to Read-Only? I’m also the publisher of the WFS, created it via GeoServer , has 58 layers in it and so on. Is there a setting within GeoServer to allow writing of data to it?

Im coding in C++ using QT Creator version 5.4.1. Using the GDAL Libraries with xerces.
My code to try to add/remove a layer

m_datasource = OGRSFDriverRegistrar::Open("Wfs here",true);
m_datasource->DeleteLayer(m_current_feature_id);
m_datasource->AddLayer("My awesome layer",0,WKBUnknown,0);

Also I just checked, via code I told Gdal to open the Data Source with the ability to write to it, gave no error meaning I can write to it then. What am I doing wrong?
Thanks

Gdal_calc works but I get a pyhon error at the end of each process

$
0
0

I have a problem while using gdal_calc by a .bat file.
Here is the code :

call “C:OSGeo4w64bino4w_env.bat”
gdal_calc -A”D:tuilage_script_1tuilagendvi4_810000_6370000_820000_6360000_ndvi.tif” –calc=”A” –outfile=”D:tuilage_script_1tuilagendvitest_gdal_cal3.tif”

It first loads OSGEO environment then it copys the input raster “A” in an output raster “outfile”. The operation is done, but I get the following error messsage : “Python has stopped working”, see picture enclosed

Screenshot

I already read this topic :
GDAL_CALC works but I get a python error at the end of each process that prevents automation

But the solution doesn’t fit with the aim of my batch file that must be automated to be runned from an office basic script for more than 100 operations.

I am using 1.11.2 Gdal version with 2.8 Qgis Wien, 2.7 Python version on Windows-7.

I would be very grateful for any idea/suggestion about this.
Thanks!

gdal2tiles only rendering half of tif

$
0
0

I am using gdal2tiles.py to render map tiles of some data.

The file I use is generated by combining a .tif and .tfw using gdal_translate XX.tif XXn.tif

Whenever I open up the file in QGIS, I see all the data:
Original data in QGIS

Next, I create tiles by gdal2tiles.py -p geodetic --s_srs "+proj=longlat +datum=WGS84 +no_defs" --srcnodata=0 final.tif and add it to my website, but half of this file is rendered. (I’ve already adjusted Leaflet to use TMS indexing.)
Rendered tiles over base layer in browser

gdalinfo dump:

Driver: GTiff/GeoTIFF
Files: XXn.tif
Size is 3600, 1800
Coordinate System is `'
Metadata:
TIFFTAG_DATETIME=2015:05:01 21:05:52
TIFFTAG_DOCUMENTNAME=XX.tif
TIFFTAG_IMAGEDESCRIPTION=IDL TIFF file
TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
TIFFTAG_SOFTWARE=IDL 8.3, Exelis Visual Information Solutions, Inc.
TIFFTAG_XRESOLUTION=100
TIFFTAG_YRESOLUTION=100
Image Structure Metadata:
INTERLEAVE=BAND
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 1800.0)
Upper Right ( 3600.0, 0.0)
Lower Right ( 3600.0, 1800.0)
Center ( 1800.0, 900.0)

Any ideas? Is this a projection issue? I haven’t worked with tif files before so I may be wrongly invoking one of these commands. I’ve also tried using gdalwarp on the file and rendering it with the above command for gdal2tiles and I still get the same issue.

ReadRaster() C#

$
0
0

I am new with GDAL in c#.

Can some body help me to uderstand the method band.ReadRaster().

I need to know wich one is the return in C# and as well some documentation about what it means each parameter of the method.

I am calculating a NDVI using GDAL.


How to average gdal_hillshades?

$
0
0

For some unknow reason, my average of 3 hillshades is very dark :

gdaldem hillshade input.tif hillshades_C.tmp.tif -s 111120 -z 5 -az 315 -alt 60 -compute_edges
gdaldem hillshade input.tif hillshades_B.tmp.tif -s 111120 -z 5 -az 355 -alt 60 -compute_edges
gdaldem hillshade input.tif hillshades_A.tmp.tif -s 111120 -z 5 -az 275 -alt 60 -compute_edges
gdal_calc.py -A hillshades_A.tmp.tif  -B hillshades_B.tmp.tif -C hillshades_C.tmp.tif --outfile=./hillshades.tmp.tif --calc="(A+B+C)/3"

enter image description here

I did tried --calc="(A+B+C)", it get lighter, but still too dark and not as expected.

How to not process when query is outside the raster extent ? (Error when Completely Outside)

$
0
0

Given a West North East South bounding box, I want to crop this data from a hundreds of inputXX.tif. Nearly every times but once, the query first print out that it “falls completely outside raster extent but is going on [processing it] however”, then uselessly start to process the out-of-range raster, spenting huge amoint of time on EACH file.

$gdal_translate -projwin 0 55 -10 45 ./data/input${i}.tif ./tmp/crop.${i}.tmp.tif
Input file size is 6001, 6001
Computed -srcwin 296400 -57000 38400 39000 from projected window.
Warning: Computed -srcwin 296400 -57000 38400 39000 falls completely outside raster extent. Going on however.
0...10...20...30...40...50...60...70...80...90...100 - done.

As I’am processing SRTM3′s 871 files via a makefile-bash loop…

#!bash
files=(`ls ../data/cgiar-csi_90/ | grep .tif$`) 
for i in "${!files[@]}"
do
    gdal_translate -projwin $WEST $NORTH $EAST $SOUTH ../data/cgiar-csi_90/${files[${i}]} ./tmp/crop.${i}.tmp.tif 
done 
gdal_merge.py -o ./crop_xl.tmp.tif ./tmp/crop.*.tmp.tif -n -32768 

…How to NOT process such raster when query is outside the raster extent ? and pass without error.

Note: gdal_translate doesn t seems to have native way to jump over such out-of-range inputs, as far as i understand.

gdal_array.SaveArray() leaves dataset open in Python

$
0
0

When I use gdal_array.SaveArray() to create a raster, the newly created dataset appears to stay open in Python, preventing other processes from working with it. For instance, consider the following (super minimal) code:

>>> a = np.arange(300).reshape((3, 10, 10))
>>> gdal_array.SaveArray(a, "test.tif")
<osgeo.gdal.Dataset; proxy of <Swig Object of type 'GDALDatasetShadow *' at 0x0458B968> >
>>> os.rename("test.tif", "test2.tif")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
WindowsError: [Error 32] The process cannot access the file because it is being used by another process

I am similarly prevented from moving, renaming, or deleting the file directly from Windows explorer, with the message The action can't be completed because the file is open in python.exe. More importantly, I can’t open the file some visualization/processing programs that I use. Once I exit python, the file is “released” and I can manipulate it to my heart’s content.

It doesn’t seem like the file has a name associated with it, so I can’t close it as I would a raster that I had specifically opened:

>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'gdal', 'gdal_array', 'np', 'os']

What causes this behavior? Is there a way to call SaveArray() such that it doesn’t keep the file opened after writing it? Or, a way to close the file from within Python?

In case it’s important, my Python bindings are from gdal 1.11.1 for Python 2.7.8 on Windows 7.

Proper projection name for NAD83 2011 revision

$
0
0

I have two PRJ files, one from ESRI and another from some custom GDAL/OGR code I wrote (pasted below). I was trying to have both work in NAD83 UTM10N with the 2011 revision. The ESRI file has 2011 in the PROJCS name string, but the GDAL/OGR one does not, even though all the values look the same. Are these in fact the same?

I couldn’t find the ESRI version on spatialreference.org, and GeoRepository suggests that the GDAL version includes the 2011 revision.

ESRI Version (esri.prj)

PROJCS["NAD_1983_2011_UTM_Zone_10N",GEOGCS["GCS_NAD_1983_2011",DATUM["D_NAD_1983_2011",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-123.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]

GDAL/OGR Version (gdal_ogr.prj)

PROJCS["NAD_1983_UTM_Zone_10N",GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-123],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["Meter",1]]

Also, here is the output for gdalsrsinfo on each file, the PROJ.4 specs are different:

$ gdalsrsinfo esri.prj

PROJ.4 : '+proj=utm +zone=10 +ellps=GRS80 +units=m +no_defs '

OGC WKT :
PROJCS["NAD_1983_2011_UTM_Zone_10N",
    GEOGCS["GCS_NAD_1983_2011",
        DATUM["NAD_1983_2011",
            SPHEROID["GRS_1980",6378137.0,298.257222101]],
        PRIMEM["Greenwich",0.0],
        UNIT["Degree",0.0174532925199433]],
    PROJECTION["Transverse_Mercator"],
    PARAMETER["False_Easting",500000.0],
    PARAMETER["False_Northing",0.0],
    PARAMETER["Central_Meridian",-123.0],
    PARAMETER["Scale_Factor",0.9996],
    PARAMETER["Latitude_Of_Origin",0.0],
    UNIT["Meter",1.0]]

$ gdalsrsinfo gdal_ogr.prj

PROJ.4 : '+proj=utm +zone=10 +datum=NAD83 +units=m +no_defs '

OGC WKT :
PROJCS["NAD_1983_UTM_Zone_10N",
    GEOGCS["GCS_North_American_1983",
        DATUM["North_American_Datum_1983",
            SPHEROID["GRS_1980",6378137,298.257222101]],
        PRIMEM["Greenwich",0],
        UNIT["Degree",0.017453292519943295]],
    PROJECTION["Transverse_Mercator"],
    PARAMETER["latitude_of_origin",0],
    PARAMETER["central_meridian",-123],
    PARAMETER["scale_factor",0.9996],
    PARAMETER["false_easting",500000],
    PARAMETER["false_northing",0],
    UNIT["Meter",1]]

Create GeoTIFF from JPG and JFW files with gdal

$
0
0

I have a JPG image and the corresponding geospatial information on a JFW wolrd file in a ETRS89 geodesical system and UTM coordinates (EPSG:25830)

5.0000000000
0.0000000000
0.0000000000
-5.0000000000
484752.5000000000
4811767.5000000000

I want to create a GeoTIFF on a SRS EPSG:4326 and extract the edge lat/lon coordinates [ulx uly lrx lry]. I am using the folowing command without success which maybe requires to do a warp before (although I don’t think so because line 2 and 3 are null on JFW file), and I do not know how to extract the edge values on that projection.

gdal_translate -of GTiff -a_srs EPSG:4326 -projwin 0 0 [width_jpg] [height_jpg] [from_file_jpg] [to_file_tif]

Does someone know how to do it? It is necessary to define width and height of image if I am using the whole image?

EDIT

The code used to display the tile is the following one:

//WMS overlay
var format = 'image/png8';
var tiled = new OpenLayers.Layer.WMS(
           "Show Layer", "http://151.80.143.120:8080/geoserver/" + workSpace + "/wms",
           {
               LAYERS: layerName,
               STYLES: '',
               TRANSPARENT: true,
               VERSION: '1.3.0',
               format: format,
               tiled: true
           },
           {
               sphericalMercator: true,
               buffer: 0,
               displayOutsideMaxExtent: true,
               isBaseLayer: false,
               opacity: 1.0,
               yx: { 'EPSG:4326': true }
           }
);
map.addLayer(tiled);

Is there an open source solution to access raster layers in an ESRI File-GeoDatabase?

$
0
0

I’m looking for a way to access raster layers in an Esri File-GeoDatabase without using ArcObjects or ArcPy, specifically I would like to access the raster layers with GDAL. I understand that the GDAL FileGDB and OpenFileGDB give access to vector datasets within File-GeoDatabases, and that ESRI’s own FileGDB C++ API does not support raster access.

Is anyone out there aware of any open source solutions? Or any ongoing efforts? I’m happy to contribute to any current efforts.

Vector to Raster using GDAL (gdal.RasterizeLayer) : Error in Output Raster(generating output raster with NAN values)

$
0
0

For rasterizing a vector layer, I have tried gdal.RasterizeLayer method for converting vector(shapefile) to raster(tiff). But its giving raster output with NAN values(complete black image). I need some help in which where I am doing wrong. And I want to print one attribute value from shapefile to raster & remaining values should be zero in outRaster.

My code ( using Gdal )is

    NoDataVal = -9999

    # Open the data source and read in the extent
    inPolygonShp = r"E:polygons.shp"
    outputRaster=r"E:OutRaster.tif"
    inGridSize=float(2)/110575      # nearly I am converting to 2 meters gridsize since its in GCS coordinates (vector layer)
    shpDS = ogr.Open(inPolygonShp)
    shpLayer = shpDS.GetLayer()

    # Create the destination data source
    xMin, xMax, yMin, yMax = shpLayer.GetExtent()
    xRes = int((xMax - xMin) / inGridSize)
    yRes = int((yMax - yMin) / inGridSize)
    rasterDS = gdal.GetDriverByName('GTiff').Create(outputRaster, xRes, yRes, 1,    gdal.GDT_Byte)

    # Define spatial reference
    rasterDS.SetProjection(shpLayer.GetSpatialRef().ExportToWkt())
    rasterDS.SetGeoTransform((xMin, inGridSize, 0, yMax, 0, -inGridSize))
    rBand = rasterDS.GetRasterBand(1)
    rBand.SetNoDataValue(NoDataVal)
    rBand.Fill(NoDataVal)

    # Rasterize
    err = gdal.RasterizeLayer(rasterDS, [1], shpLayer, burn_values=[200], options = ["ALL_TOUCHED=TRUE"])

    # for rasterizing with a attribute value of polygon
    # err = gdal.RasterizeLayer(rasterDS, [1], shpLayer, burn_values=[0], options = ["ATTRIBUTE= Height"])

Here I feel its generating a raster file but its failed to overwrite with shapefile values.
I need some help in this regard.


How to make a movie from a ascii grid files?

$
0
0

Lets say I have several ascii grid files for each time step for a particular location. I would like to spit out the image for each of these ascii grid files and legend on the side bar. I also would like to put the area boundary (County or state) on the top of every image. After that I would like to make a movie from all of these images. How to procede this task in python or R?

NCOLS xxx
NROWS xxx
XLLCENTER xxx | XLLCORNER xxx
YLLCENTER xxx | YLLCORNER xxx
CELLSIZE xxx
NODATA_VALUE xxx
row 1
row 2
...
row n

GDAL raster shifting with python error: 'NoneType' object has no attribute 'GetGeoTransform'

$
0
0

I’m trying to shift a georeferenced raster by 0.5m N and 0.5m W in QGIS using the python console:

from osgeo import gdal

# Open in read/write mode
rast_src = gdal.Open('test_raster.tif', 1)

# Get affine transform coefficients
gt = rast_src.GetGeoTransform()

# Convert tuple to list, so we can modify it
gtl = list(gt)
gtl[0] -= 0.5  # Move west 0.5 m
gtl[3] += 0.5 # Move north 0.5 m

# Save the geotransform to the raster
rast_src.SetGeoTransform(tuple(gtl))
rast_src = None # equivalent to save/close

However line #7 (gt = rast_src.GetGeoTransform()) throws an AttributeError: ‘NoneType’ object has no attribute ‘GetGeoTransform’. The installed GDAL version is 1.11.0. The raster seems fine. So what’s wrong?

Read raster at request size through Python

$
0
0

I’m trying to read the raster (tif) file using Python.
What I want is to read the raster at a specific request size rather than its original resolution.

Example: If a raster has a cellsize of 5 x 5, I would want to read the raster at a lower resolution of suppose 50 x 50 which would reduce the total number of cols and rows. Basically read the pyramid of the raster which has the closet number cols to that of the request size.

Is there a way to read the raster at a particular request size specified through the ArcPy module or gdal module?

Add hillshade to naip imagery

$
0
0

I’ve been searching for quite a while now. Is there a way to merge a hillshade image with an aerial image with gdal?
I’m just trying to create a hillshaded aerial image.

Minidump on exit in QGIS (2.8.2- Wien) 64-bit version when using GDAL in plugin

$
0
0

I am working on a plugin for QGIS that is working fine on 32-bit versions but I am having some problems while using it on the 64-bit standalone installation package.
I have tried on several machines and on all of them QGIS locks up and creates a minidump on exit when certain parts of the code in the plugin has been ran. The plugin still works, the only problem seems to be the crash dump on exit.

As far as I have been able to tell this line of code using the gdal-module seems to be causing the crash dumps:

    gdal_dsm = gdal.Open(filepath_dsm)
    dsm = gdal_dsm.ReadAsArray().astype(numpy.float) - this line of code is the offending one.
    ...
    gdal_dsm = None
    dsm = None

Am I doing something wrong or is it a problem with the 64-bit standalone installation of QGIS? Is there anyway to avoid the crash dump on exit for potential users of the plugin that might be using this installation package?

Viewing all 397 articles
Browse latest View live