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

gdal_calc : fails with value 0?

$
0
0

I working to invert values of a one-band tif files (download sample).

If i use 254 it works:

$ gdal_calc.py -A ./shadedrelief.tmp.tif  --outfile=whited.tmp.tif  --calc="254*(A>180)" 
0 .. 10 .. 20 .. 30 .. 40 .. 50 .. 60 .. 70 .. 80 .. 90 .. 100 - Done
$ gdallocationinfo -valonly ./whited.tmp.tif 1 1   # we expect 254
254
$ gdal_calc.py -A whited.tmp.tif --outfile=inverted.tif --calc="255-A"
0 .. 10 .. 20 .. 30 .. 40 .. 50 .. 60 .. 70 .. 80 .. 90 .. 100 - Done
$ gdallocationinfo -valonly ./inverted.tif 1 1   # we expect 1 (because 255-A = 255-254)
1 

But the value 255 is troublesome:

$ gdal_calc.py -A ./shadedrelief.tmp.tif  --outfile=whited.tmp.tif  --calc="255*(A>180)" 
0 .. 10 .. 20 .. 30 .. 40 .. 50 .. 60 .. 70 .. 80 .. 90 .. 100 - Done
$ gdallocationinfo -valonly ./whited.tmp.tif 1 1   # we expect 255 
255
$ gdal_calc.py -A whited.tmp.tif --outfile=inverted.tif --calc="255-A"
0 .. 10 .. 20 .. 30 .. 40 .. 50 .. 60 .. 70 .. 80 .. 90 .. 100 - Done
$ gdallocationinfo -valonly ./inverted.tif 1 1   # we expect 0 (because 255-A = 255-255)
255    # <============================ THIS OUTPUT IS NOT EXPECTED.

As far as I know, RGBA range is [0-255]. So…

Why does gdal_calc fails with value 0?


Edit: and for some reason, the 254-based whited.tmp.tif only has 2 type of pixels, so end final.tif also only has transparent [255,0] or black [0,255]. I expected a continuous gradation between the both.

enter image description here


Reprojecting a raster image using gdalwarp

$
0
0

I am trying to reproject an ECW Orthophoto from 25830 to 23030 srid.
I tried the following but I am getting an error:

C:Program Files (x86)FWTools2.4.7>gdalwarp -s_srs 25830 -t_srs 23030 C:orto.e
cw C:orto_ed50.ecw
ERROR 1: Translating source or target SRS failed:
25830

Most efficient way to retrieve pixel distributions inside many polygones (custom zonal stats)

$
0
0

I want to get the distribution of pixels from a 1 band raster that fall in each polygon of a shapefile to proceed to some statistics. I guess a zonal statistics does it in some kind of way but I need other statitsics that the ones provided by the tools.

Specifically, I want to sort the pixels inside the polygons and retrieve the 50th value (if exists).

Converting my raster to points and joining them spatially to my polygons and exporting my table for the statistical analysis would do it but I will obtain more than 80M points and I estimate that it will take more than 4 days if no issues.

Does anyone know how to perform this in a more efficient way ?

“Too many points” when using gdal2tiles.py in EPSG:4326

$
0
0

I want to put a custom map onto cartodb, which, I believes, requires EPSG 4326. When I try making tiles with gdal2tiles, using a 4326 georeferenced image, I get the following error:

ERROR 1: Too many points (10201 out of 10201) failed to transform,
unable to compute output bounds.

If I try to use other projections, gdal2tiles will work fine but they show up in the completely wrong place on cartodb.

GRASS GIS r.slope.aspect write errors

$
0
0

I am running into an error running r.slope.aspect I have never seen before. When I run it, I get the following output:

r.in.gdal complete. Raster map <dem> created.
WARNING: map [slope] - unable to write row 1269
WARNING: map [aspect] - unable to write row 1270
100%    
WARNING: error writing null row 1283
WARNING: error writing null row 1284
WARNING: error writing null row 1285

The “error writing null row” continues for a very long time. I have been running this script many times and only recently am I starting to get this strange error.

echo "Running r.sun for day $DAY"
#Create new projection info
g.proj -c georef=$DEM
#Import Dem
g.mremove -f "*"
r.in.gdal input=$DEM output=dem
#Set Region
g.region -s rast=dem
#Calculate Slope and Aspect
r.slope.aspect elevation=dem slope=slope aspect=aspect
#Create flat map
r.mapcalc "zeros=if(dem>0,0,null())"
r.sun elevin=dem aspin=zeros slopein=zeros day=$DAY step=$STEPSIZE dist=$INTERVAL glob_rad=flat_total_sun
#Using dem and calculated slope and aspect, generate a solar insulation model
r.sun elevin=dem aspin=aspect slopein=slope day=$DAY step=$STEPSIZE dist=$INTERVAL insol_time=hours_sun glob_rad=total_sun
#Output files
r.out.gdal -c input=total_sun output=./global/daily/total_sun_day_${DAY}.tif
r.out.gdal -c input=flat_total_sun output=./global/daily    /flat_total_sun_day_${DAY}.tif
r.out.gdal -c input=hours_sun output=./insol/daily/hours_sun_day_${DAY}.tif
r.out.gdal -c input=slope output=./slope.tif
r.out.gdal -c input=aspect output=./aspect.tif

Problem Installing GDAL extension for Geoserver – java.lang.UnsatisfiedLinkError

$
0
0

As the title states im getting an error trying to install the GDAL extension for Geoserver 2.7 (My goal here is to enable MrSID support).

The complete error message is:

java.lang.UnsatisfiedLinkError: C:gdal-nativeLibgdaljni.dll: Can’t
load AMD 64-bit .dll on a IA 32 bit platform

I am on a windows 7 x64 machine. I went through the intructions located here, setting my environmental variables as described. From what I googled it may be related to my JRE version? Any help is much appreciated.

How can I merge multiple grib2 files into a single file with a time dimension that I can upload to Geoserver?

$
0
0

I have a large number of grib2 files representing the same grid for different times. I want to be able to select an arbitrary amount of them and merge them into a single grid with time dimension. That would be uploaded to Geoserver and then I’d pull them down into my World Wind application. Ideally the workflow would be something like select the times you want in a java list, merge the time grids into a single grib or geotiff or whatever, upload the coverage to Geoserver and then pull them down as layers into World Wind.

For a snapshot of the tools I’m using now or have access to

World Wind
GDAL java bindings
Geotools java libraries
Geoserver 2.7

I’m trying to avoid using command line tools like wgrib2 or cdo since this is supposed to be a user friendly application.

QGIS downsample then upsample raster

$
0
0

I’m trying to downsample a raster then upsample it again.

I am using the gdalwarp function in QGIS.

When downsampling the raster looks OK in the front end, I set the cell size to 10x the original raster.

When upsampling back to the original cell size using the downsampled raster the original data seems to be preserved and I end up back at the original raster. What I’m expecting to happen is that I see blocks of 10×10 cells all with the same value (same as the downsampled raster).

Does QGIS preserve the original cell values? Is there a way to perform this operation?

Thanks


GetRasterBand() method for gdal in Python

$
0
0

I was going through a tutorial book called Python Geospatial Development. On the chapter on using working with geospatial data in python there is an example of a script meant to handle and analysis raster data for the height values. I ran the following code:

import sys, struct
from osgeo import gdal
from osgeo import gdalconst


minLat = -48
maxLat = -33
minLong = 165
maxLong = 179

dataset = gdal.Open("l10g")
band = dataset.GetRasterBand(1)

t = dataset.GetGeoTransform()
success,tInverse = gdal.InvGeoTransform(t)
if not success:
    print("Failed!")
    sys.exit(1)

x1, y1= gdal.ApplyGeoTransform(tInverse, minLong, minLat)
x2, y2= gdal.ApplyGeoTransform(tInverse, maxLong, maxLat)

minX = int(min(x1, x2))
maxX = int(max(x1, x2))
minY = int(min(y1, y2))
maxY = int(max(y1, y2))

width = (maxX - minX) + 1
fmt = "<" + ("h"* width)

for y in range(minY, maxY+1):
    scanline = band.ReadRaster(minX, y, width, 1, width, 1,
                               gdalconst.GDT_Int16)
    values = struct.unpack(fmt, scanline)

    for values in values:
        try:
            histogram[value] += 1
        except KeyError:
            histogram[value] = 1
for height in sorted(histogram.keys()):
    print (height, hsitogram[height])

but I got the following error:

Traceback (most recent call last):
  File "D:PythonProgiesGeospatiall10ghistogram.py", line 12, in <module>
    band = dataset.GetRasterBand(1)
AttributeError: 'NoneType' object has no attribute 'GetRasterBand'

Shapefile to Mysql error using ogr2ogr

$
0
0

I’m following this tutorial. I installed the “GDAL Complete” Framework from kyngchaos.com and I tested the installation on Mac. I have GDAL 1.11.2, released 2015/02/10.

When I am trying to run this command:

ogr2ogr -f MySQL MySQL:dbname,user=root,password=password 
      -nln world_borders -nlt MULTIPOLYGON 
      -update -overwrite 
      -lco ENGINE=MyISAM 
      -lco MYSQL_FID=ogr_fid -lco GEOMETRY_NAME=geometry 
      /path/to/TM_WORLD_BORDERS-0.3.shp

With my settings it would be:

ogr2ogr -f MySQL MySQL:mapas,user=xxxx,password=xxxx 
      -nln test -nlt MULTIPOLYGON 
      -update -overwrite 
      -lco ENGINE=MyISAM 
      -lco MYSQL_FID=ogr_fid -lco GEOMETRY_NAME=geometry 
      /Users/Seph/Downloads/TM_WORLD_BORDERS-0.3/TM_WORLD_BORDERS-0.3.shp

I got this error:

Unable to find driver `MySQL'.
The following drivers are available:
-> `ESRI Shapefile'
-> `MapInfo File'
-> `UK .NTF'
-> `SDTS'
-> `TIGER'
-> `S57'
-> `DGN'
-> `VRT'
-> `REC'

I have created the table on my database before running the command.

UPDATE
I installed Homebrew I and ran this command:

brew install gdal --with-mysql

Now I have the supported format: -> “MySQL” (read/write)

But now I have this error:

Macbook-pro:~ Seph$ ogr2ogr -f MySQL MySQL:mapas,user=xxxx,password=xxxx
      -nln test -nlt MULTIPOLYGON 
      -update -overwrite 
      -lco ENGINE=MyISAM 
      -lco MYSQL_FID=ogr_fid -lco GEOMETRY_NAME=geometry 
       /Users/Seph/Downloads/TM_WORLD_BORDERS-0.3/TM_WORLD_BORDERS-0.3.shp
      ERROR 1: MySQL connect failed for: mapas,user=xxxx,password=xxxx
      Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
      ERROR 1: MySQL connect failed for: mapas,user=xxxx,password=xxxx
      Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
      ERROR 1: MySQL connect failed for: mapas,user=xxxx,password=xxxx
      Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
      ERROR 1: MySQL driver doesn't currently support database creation.
      Please create database before using.
      MySQL driver failed to create MySQL:mapas,user=xxxx,password=xxxx

I am using phpMyAdmin from Xammp

Python GDAL: ogr.open or driver.open?

$
0
0

I’ve been using GDAL with python to manipulate shapefiles. When opening a source file I first set the driver to “ESRI Shapefile” before calling the driver’s “Open” method with the required shapefile.

I have had no issues with this; however I am curious what the difference is between the Driver instance method “Open” and the ogr function “Open”. For example, what is the difference between:

driver = ogr.GetDriverByName("ESRI Shapefile")
source = driver.Open(“O:myshp.shp”,0)

and

source = ogr.Open(“O:myshp.shp”,0)

Is there a particular scenario where you would use one over the other?

Qgis 2.8, gdal python scripts gives error at end of process

$
0
0

I am using Qgis 2.8.1 64-bit (Standalone installer) which also packages a number or other applications, like OSGeo4W Shell, Saga GIS et.c.

In the OSGeo4W Shell I am encountering a problem with some gdal/ogr python scripts that seem identical to what is described here in another question: link

The problem seems to be restricted to gdal2tiles and gdal_calc, I have done some quick testing and the problem doesn’t seem to exist for other scripts.

What causes this error and how can it be resolved?

System information:
Path in OSGeo4W Shell:
C:PROGRA~1QGISWI~1appsPython27Scripts;C:PROGRA~1QGISWI~1bin;C:Windowssystem32;C:Windows;C:WindowsWBem;
C:PROGRA~1QGISWI~1appsmsysbin

pythonhome:
C:PROGRA~1QGISWI~1appsPython27

gdal_data:
C:PROGRA~1QGISWI~1sharegdal

converting grib to netcdf with time dimension

$
0
0

I’m trying to convert grib files to NetCDF. The grib files contains subdata sets which are time related. Using the netcdfAll Java library with the NetCDF-4 C library works fine.

java -Xmx1g -classpath netcdfAll-4.5.jar ucar.nc2.dataset.NetcdfDataset -in ECM_DSD_2015021700_0000 -out ECM_DSD_2015021700_0000.nc -isLargeFile -netcdf4

The resulting NetCDF-4 file:

gdalinfo ECM_DSD_2015021700_0000.nc is listing all subdata sets like
Subdatasets:
  SUBDATASET_1_NAME=HDF5:"ECM_DSD_2015021700_0000.nc"://GaussianLatLon_1280X2560-p07028S-179p9W/100_metre_U_wind_component_surface
  SUBDATASET_1_DESC=[1x1280x2560] //GaussianLatLon_1280X2560-p07028S-179p9W/100_metre_U_wind_component_surface (32-bit floating-point)
  SUBDATASET_2_NAME=HDF5:"ECM_DSD_2015021700_0000.nc"://GaussianLatLon_1280X2560-p07028S-179p9W/100_metre_V_wind_component_surface
  SUBDATASET_2_DESC=[1x1280x2560] //GaussianLatLon_1280X2560-p07028S-179p9W/100_metre_V_wind_component_surface (32-bit floating-point)

The metatada of one subset shows an empty dimension list and a missing time dimension:

gdalinfo HDF5:"ECM_DSD_2015021700_0000.nc"://GaussianLatLon_1280X2560-p07028S-179p9/100_metre_U_wind_component_surface
Band 1 Block=2560x25 Type=Float32, ColorInterp=Undefined
  Min=-25.970 Max=30.284
  Minimum=-25.970, Maximum=30.284, Mean=0.493, StdDev=7.106
  Metadata:
    GaussianLatLon_1280X2560-p07028S-179p9W_100_metre_U_wind_component_surface__Netcdf4Dimid=15
    GaussianLatLon_1280X2560-p07028S-179p9W_100_metre_U_wind_component_surface_coordinates=time
    GaussianLatLon_1280X2560-p07028S-179p9W_100_metre_U_wind_component_surface_DIMENSION_LIST=
    GaussianLatLon_1280X2560-p07028S-179p9W_100_metre_U_wind_component_surface_Grib1_Center=98
    GaussianLatLon_1280X2560-p07028S-179p9W_100_metre_U_wind_component_surface_Grib1_Level_Desc=Ground or water surface
    GaussianLatLon_1280X2560-p07028S-179p9W_100_metre_U_wind_component_surface_Grib1_Level_Type=1
    GaussianLatLon_1280X2560-p07028S-179p9W_100_metre_U_wind_component_surface_Grib1_Parameter=246
    GaussianLatLon_1280X2560-p07028S-179p9W_100_metre_U_wind_component_surface_Grib1_Parameter_Name=100u
    GaussianLatLon_1280X2560-p07028S-179p9W_100_metre_U_wind_component_surface_Grib1_Subcenter=0
    GaussianLatLon_1280X2560-p07028S-179p9W_100_metre_U_wind_component_surface_Grib1_TableVersion=228
    GaussianLatLon_1280X2560-p07028S-179p9W_100_metre_U_wind_component_surface_Grib_Variable_Id=VAR_98-0-228-246_L1
    GaussianLatLon_1280X2560-p07028S-179p9W_100_metre_U_wind_component_surface_long_name=100 metre U wind component @ Ground or water surface
    GaussianLatLon_1280X2560-p07028S-179p9W_100_metre_U_wind_component_surface_missing_value=1.#QNAN
    GaussianLatLon_1280X2560-p07028S-179p9W_100_metre_U_wind_component_surface_units=m s**-1
    STATISTICS_MAXIMUM=30.284362792969
    STATISTICS_MEAN=0.49316239946346
    STATISTICS_MINIMUM=-25.969543457031
    STATISTICS_STDDEV=7.1061257055032

So what is the trick to convert the data with the time dimension? I found some python scripts[1] and the ncks tool[2], need I switch to one of them?

After I want to convert each subdataset to a single GeoTiff but this should be more easy when the time dimension was rescued once :-)

I am working with GDAL 1.11 and netcdfAll 4.5

Thanks for any help and best regards,
Tee

[1] http://pysclint.sourceforge.net/pycdf/pycdf.html and https://readchunks.wordpress.com/
[2] http://linux.die.net/man/1/ncks

creating a random raster image using gdal

$
0
0

I want to create a random image containing say three classes with predefind percentages of each of the classes using gdal in python.Any sample code would be very appreciated.

GDAL doesnot support HDF4 e [duplicate]


GDAL merge; Why add 0.1?

OSGeo4W and GDAL – gdal2tiles.py error

$
0
0

Wondering why gdal2tiles is throwing errors, I’ve added the gdaldata and gdalplugins environmental variables and my python is at Python27/ArcGIS10.3/, and am wondering if that is messing with the OSGeo4W python located in the C:OSGeo4Wbin folder, or do I need to direct my environment variables every time I want to use OSGeo4W vs. ArcGIS python versions? Or is it working and the error I’m seeing below has to do with something else totally seperate?

This is really confusing to me, thanks! –and the stackexchange tag should be “gdal2tiles.py” there is only “gdal2tile” available…I don’t have enough cred to add one

OSGEO4W home is C:OSGeo4W

                   -={ OSGeo4W Shell Commands }=-

       bmp2tiff             listgeo              raw2tiff
       cs2cs                nad2bin              rgb2ycbcr
       curl                 ncdump               rpcgen
       dllupdate            ncgen                rpcinfo
       fax2ps               nearblack            testepsg
       fax2tiff             nircmd               textreplace
       gdaladdo             nircmdc              thumbnail
       gdalbuildvrt         ogdi_import          tiff2bw
       gdaldem              ogdi_info            tiff2pdf
       gdalenhance          ogr2ogr              tiff2ps
       gdalinfo             ogrinfo              tiff2rgba
       gdallocationinfo     ogrlineref           tiffcmp
       gdalmanage           ogrtindex            tiffcp
       gdalserver           opj_compress         tiffcrop
       gdalsrsinfo          opj_decompress       tiffdither
       gdaltindex           opj_dump             tiffdump
       gdaltransform        osgeo4w-setup-work   tiffinfo
       gdalwarp             osgeo4w-setup        tiffmedian
       gdal_contour         pal2rgb              tiffset
       gdal_grid            portmap              tiffsplit
       gdal_rasterize       ppm2tiff             w9xpopen
       gdal_translate       proj                 xmlcatalog
       geod                 psql                 xmllint
       geotifcp             python               xxmklink
       gif2tiff             pythonw
       gltpd                ras2tiff


       epsg_tr           gdalmove          make-bat-for-py
       esri2wkt          gdal_auth         mkgraticule
       gcps2vec          gdal_calc         o-help
       gcps2wld          gdal_edit         o4w_env
       gdal2tiles        gdal_fillnodata   pct2rgb
       gdal2xyz          gdal_merge        rgb2pct
       gdalchksum        gdal_polygonize   setup-test
       gdalcompare       gdal_proximity    setup
       gdalident         gdal_retile
       gdalimport        gdal_sieve

GDAL 1.11.2, released 2015/02/10

C:UsersmaugustDesktop>gdal2tiles.py C:UsersmaugustDesktopexample_tifsPie
rce_2010_84.tif C:UsersmaugustDesktopexample_tifstilegdal
Traceback (most recent call last):
  File "C:OSGeo4Wbingdal2tiles.py", line 2278, in <module>
    gdal2tiles.process()
  File "C:OSGeo4Wbingdal2tiles.py", line 482, in process
    self.open_input()
  File "C:OSGeo4Wbingdal2tiles.py", line 856, in open_input
    self.out_ds.SetMetadataItem('NODATA_VALUES','%i %i %i' % (self.in_nodata[0],
self.in_nodata[1],self.in_nodata[2]))
IndexError: list index out of range

C:UsersmaugustDesktop>

How to define PROJ.4 string for GDAL projection

$
0
0

I’m doing some zonal computations in GDAL using the code posted in the solution here: Issue Trying to create Zonal Statistics using Gdal and Python.

It runs fine for me using the example data posted with the question, but when I try with my own GeoTiff and shapefile, this step:

# Read projection info from input GeoTiff
raster_srs.ImportFromWkt(raster.GetProjectionRef())
target_ds.SetProjection(raster_srs.ExportToWkt())

# Rasterize zone polygon to raster
gdal.RasterizeLayer(target_ds, [1], lyr, burn_values=[1])

throws the following error:

ERROR 1: No PROJ.4 translation for destination SRS, coordinate
transformation initialization has failed.

gdal info:

Driver: GTiff/GeoTIFF
Files: data/ndvi/ndvi_c_dk_20150405.tif
Size is 29424, 30470
Coordinate System is:
LOCAL_CS["unnamed",
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]]]
Origin = (-19.000000000000000,37.999585000000003)
Pixel Size = (0.002413000000000,-0.002413000000000)
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  INTERLEAVE=BAND
Corner Coordinates:
Upper Left  ( -19.0000000,  37.9995850) 
Lower Left  ( -19.0000000, -35.5245250) 
Upper Right (  52.0001120,  37.9995850) 
Lower Right (  52.0001120, -35.5245250) 
Center      (  16.5000560,   1.2375300) 
Band 1 Block=29424x1 Type=Byte, ColorInterp=Palette
  Color Table (RGB with 256 entries)

Knowing that the coordinate systems info is missing, I think I should explicitly define the Proj4 string. I’ve found examples of doing such like this:

wgs84_wkt = """
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.01745329251994328,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]"""

then I presume I could do

raster_srs.ImportFromWkt(wgs84_wkt)
target_ds.SetProjection(raster_srs.ExportToWkt())

But I have no idea how to define the correct Proj4 string in the first place.

Is there a URL I can post to my geoserver that confirms that I properly installed the GDAL plugin?

$
0
0

I’m running geoserver in Tomcat and went through the process of installing the plugin for GDAL formats. I specifically need NITF.

Is there a URL I can post to my geoserver that confirms that I properly installed the GDAL plugin?

Suppress verbose output when something goes wrong with ogr2ogr

$
0
0

Whenever ogr2ogr is not able to read a file, it seems to produce pages of output:

Unable to open datasource `file.geojson' with the following drivers.
  -> ESRI Shapefile
  -> MapInfo File
  -> UK .NTF
  -> SDTS
  -> TIGER
  -> S57
  -> DGN
  -> VRT
  -> REC
  -> Memory
  -> BNA
  -> CSV
  -> NAS
  -> GML
  -> GPX
  -> LIBKML
...

Is there a way to suppress this incredibly annoying output? My workaround is:

ogr2ogr ... 2>&1 | grep -v '^  ->'
Viewing all 397 articles
Browse latest View live