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

Some tiff not included on a vrt index

$
0
0

I have some tiff files and I’m trying to generate a vrt index with gdal tool, gdalbuildvrt.

I run this command:

gdalbuildvrt orto_index.vrt -srcnodata "0 0 0" -vrtnodata "0 0 0" *.tif

and throws the next warning:

Warning 6: gdalbuildvrt does not support rotated geo transforms.
Skipping 2335-C.tif

So there are some tiff that are skipped.

If we compare the information about a skipped file with another that it has not skipped, we could see the next difference:

In the skipped file, we can see the next line:

GeoTransform =
  345770.7884398972, 0.6000000238418568, 3.473043927400512e-15
  6139634.558238117, 1.757016748640284e-14, -0.6000000238417672

In the non skipped file, the other one:

Origin = (223499.236031105130678,6213152.835421450436115)
Pixel Size = (0.600000023841858,-0.600000023841858)

How can I make to get a vrt file including all files, I mean, without skip some files?


How to retrieve PROJECTION value for a map?

$
0
0

I am trying to read the Projection from Geotiff/shape file, using gdal 1.9.0 by the following command in the command line…

gdalinfo.exe -proj4 map-path

but its retuning empty string for “PROJECTION” parameter.. when I check the same map in QGIS getting the correct projection for this map.

Actually I want to retrieve “PROJECTION” value for any map from the C/C++ code , could you please let me know the way to retrieve this ?

Thanks,
Prady.

Can't convert geometry from Dgn file to Shape file?

$
0
0

I am using below codes to try convert geometry from a Dgn file to Shape file. But, I can’t exit the Feature reading loop. Are there anyone know why and how to fix it ? Please download the dgn file from the link: DgnFile

bool Convert()
{

    OGRRegisterAll();

    OGRDataSource       *poDS,  *poSHPDS;
    const char *pszDriverName = "ESRI Shapefile";
    OGRSFDriver *poDriver;

    poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(
                  pszDriverName );

    // Open file DGN

    poDS = OGRSFDriverRegistrar::Open( sourceFile, FALSE );

    if( poDS == NULL )

    {
         cout << "Error opeing DGN file." << endl;
         return false;
    } 
    // Create datasource for file shp
  poSHPDS = poDriver->CreateDataSource(destinationPath,NULL);

  if( poSHPDS == NULL )

  {

        cout << "Error creating shape file." << endl;

        return false;
  }


  OGRLayer  *poLayer, *poSHPLayer; 

  // Get Layer from DGN file

  poLayer = poDS->GetLayer(0);

  // Create new Layer of SHP file

  poSHPLayer = poSHPDS->CreateLayer (shpFileName, NULL, wkbPolygon, NULL);

  if(poSHPLayer == NULL)

  {

          cout << "Error creating Layer of Shape File." << endl;

          return false;
  }

  OGRFeature *poFeature;

  poLayer->ResetReading(); 

  **while( (poFeature = poLayer->GetNextFeature()) != NULL ) // Can't exit this loop** 

  {

        OGRGeometry *poGeometry;
        poGeometry = poFeature->GetGeometryRef();

        // if feature is a Polygon
        if( poGeometry != NULL && (wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon ) )
        {
                OGRFeature *poSHPFeature;
                //Create new Feature shape file
                poSHPFeature = OGRFeature::CreateFeature (poSHPLayer->GetLayerDefn());
                poSHPFeature->SetGeometry (poGeometry);
                OGRPolygon *poPolygon = (OGRPolygon*) poSHPFeature->GetGeometryRef();

                poLayer->SetSpatialFilter(poGeometry);
                poLayer->ResetReading ();

         }
         OGRFeature::DestroyFeature( poFeature );

    }
    OGRDataSource::DestroyDataSource( poDS );
    OGRDataSource::DestroyDataSource( poSHPDS );
    return true;
} 

Thanks and regards,

Tai

Why does the Clipper tool lack the mask layer option?

$
0
0

Fairly new at QGIS, but everything’s gone well so far.

I’m running QGIS version 1.8 on a Mac (OSX 10.8) and GDAL 1.10, and other GDAL tools (like Merge and Georeferencer) work just fine.

When I try to use the Clipper tool to clip a raster image using a vector shp file, I don’t see the dialog options that are shown on various forums (for example, here). There is no “No data value” control and under Clipping Mode I just have the option to clip to a rectangular extent. What am I doing wrong?

Can't convert geometry from Dgn file to Shape file?

$
0
0

I am using below codes to try convert geometry from a Dgn file to Shape file. But, I can’t exit the Feature reading loop. Are there anyone know why and how to fix it ? Please download the dgn file from the link: DgnFile

bool Convert()
{

    OGRRegisterAll();

    OGRDataSource       *poDS,  *poSHPDS;
    const char *pszDriverName = "ESRI Shapefile";
    OGRSFDriver *poDriver;

    poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(
                  pszDriverName );

    // Open file DGN

    poDS = OGRSFDriverRegistrar::Open( sourceFile, FALSE );

    if( poDS == NULL )

    {
         cout << "Error opeing DGN file." << endl;
         return false;
    } 
    // Create datasource for file shp
  poSHPDS = poDriver->CreateDataSource(destinationPath,NULL);

  if( poSHPDS == NULL )

  {

        cout << "Error creating shape file." << endl;

        return false;
  }


  OGRLayer  *poLayer, *poSHPLayer; 

  // Get Layer from DGN file

  poLayer = poDS->GetLayer(0);

  // Create new Layer of SHP file

  poSHPLayer = poSHPDS->CreateLayer (shpFileName, NULL, wkbPolygon, NULL);

  if(poSHPLayer == NULL)

  {

          cout << "Error creating Layer of Shape File." << endl;

          return false;
  }

  OGRFeature *poFeature;

  poLayer->ResetReading(); 

  **while( (poFeature = poLayer->GetNextFeature()) != NULL ) // Can't exit this loop** 

  {

        OGRGeometry *poGeometry;
        poGeometry = poFeature->GetGeometryRef();

        // if feature is a Polygon
        if( poGeometry != NULL && (wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon ) )
        {
                OGRFeature *poSHPFeature;
                //Create new Feature shape file
                poSHPFeature = OGRFeature::CreateFeature (poSHPLayer->GetLayerDefn());
                poSHPFeature->SetGeometry (poGeometry);
                OGRPolygon *poPolygon = (OGRPolygon*) poSHPFeature->GetGeometryRef();

                poLayer->SetSpatialFilter(poGeometry);
                poLayer->ResetReading ();

         }
         OGRFeature::DestroyFeature( poFeature );

    }
    OGRDataSource::DestroyDataSource( poDS );
    OGRDataSource::DestroyDataSource( poSHPDS );
    return true;
} 

Thanks and regards,

Tai

How to set a spatial filter with Python/GDAL?

$
0
0

Is there a way with GDAL (Python) to set a spatial filter (bounding-box) on a raster before I process it? I want to read it as an array.

BandReadAsArray(band, xoff=0, yoff=0, win_xsize=None, win_ysize=None, buf_xsize=None, buf_ysize=None, buf_obj=None)

Can't convert geometry from Dgn file to Shape file?

$
0
0

I am using below codes to try convert geometry from a Dgn file to Shape file. But, I can’t exit the Feature reading loop. Are there anyone know why and how to fix it ? Please download the dgn file from the link: DgnFile

bool Convert()
{

    OGRRegisterAll();

    OGRDataSource       *poDS,  *poSHPDS;
    const char *pszDriverName = "ESRI Shapefile";
    OGRSFDriver *poDriver;

    poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(
                  pszDriverName );

    // Open file DGN

    poDS = OGRSFDriverRegistrar::Open( sourceFile, FALSE );

    if( poDS == NULL )

    {
         cout << "Error opeing DGN file." << endl;
         return false;
    } 
    // Create datasource for file shp
  poSHPDS = poDriver->CreateDataSource(destinationPath,NULL);

  if( poSHPDS == NULL )

  {

        cout << "Error creating shape file." << endl;

        return false;
  }


  OGRLayer  *poLayer, *poSHPLayer; 

  // Get Layer from DGN file

  poLayer = poDS->GetLayer(0);

  // Create new Layer of SHP file

  poSHPLayer = poSHPDS->CreateLayer (shpFileName, NULL, wkbPolygon, NULL);

  if(poSHPLayer == NULL)

  {

          cout << "Error creating Layer of Shape File." << endl;

          return false;
  }

  OGRFeature *poFeature;

  poLayer->ResetReading(); 

  **while( (poFeature = poLayer->GetNextFeature()) != NULL ) // Can't exit this loop** 

  {

        OGRGeometry *poGeometry;
        poGeometry = poFeature->GetGeometryRef();

        // if feature is a Polygon
        if( poGeometry != NULL && (wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon ) )
        {
                OGRFeature *poSHPFeature;
                //Create new Feature shape file
                poSHPFeature = OGRFeature::CreateFeature (poSHPLayer->GetLayerDefn());
                poSHPFeature->SetGeometry (poGeometry);
                OGRPolygon *poPolygon = (OGRPolygon*) poSHPFeature->GetGeometryRef();

                poLayer->SetSpatialFilter(poGeometry);
                poLayer->ResetReading ();

         }
         OGRFeature::DestroyFeature( poFeature );

    }
    OGRDataSource::DestroyDataSource( poDS );
    OGRDataSource::DestroyDataSource( poSHPDS );
    return true;
} 

Thanks and regards,

Tai

Qgis – raster values become integer when Polygonizing

$
0
0

When using the GDAL plugin, “Polygonize (raster to vector)” any values in the resulting vector layer is truncated to integer.

I have a raster showing windspeeds in squares of 500*500 meters, in the metadata-field i read that the lowest value is 2.5899999142 and the highest value is 12.6599998474.
Then when polygonizeing, the result layer only has values: 0, 1, 2, 3… and so on. No decimals.

I have googled the issue and found this: http://trac.osgeo.org/postgis/ticket/650 Don’t know if it helps though.


Link GDAL to consume a WFS

$
0
0

Im using a WFS that is openly available as a demo from mapserver’s website

http://demo.mapserver.org/cgi-bin/wfs?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetCapabilities

And now i want to make a client application that can consume this service in C++ (QT) using the GDAL libraries.

I have compiled GDAL with xerces and curl as the docs say but apart from that they are not very clear about how to use it for a WFS, im sure the functionality is there because it says it supports it but i cant find any WFS specific documentation so far i have just tried to open a connection and get this error:

GDALAllRegister();
const char *pszFilename = NULL;
pszFilename = "http://demo.mapserver.org/cgi-bin/wfs?";
m_dataset = (GDALDataset*) GDALOpen(pszFilename, GA_ReadOnly);

if( m_dataset == NULL )
{
    std::cout << "Dataset is empty" << std::endl;
    exit( 1 );
}

Error:

ERROR 4: `/vsimem/http_1/file.dat' not recognised as a supported file format.

Dataset is empty
ERROR 4: Failed to create temporary file:/tmp/file.dat

I have also tried instead of the URL, an XML document that contains the URl like the docs say this shoudl work

<OGRWFSDataSource>
    <URL>http://path/to/WFS/service[?OPTIONAL_PARAMETER1=VALUE[&amp;OPTIONNAL_PARAMETER2=VALUE]]</URL>
</OGRWFSDataSource> 

but this just says the file isnt a compatible type, any help would be appreciated here

Error output with gdalwarp

$
0
0

Following is my raster file and mask shape file

files

I use qgis 1.8.0 to clip the raster with shape file by gdal tools

but after I run warp in gdal tools it’s just give me no data in the output GeoTiff?

Error during conversion of shapefile to mysql using ogr2ogr

$
0
0

I have installed ms4w in Windows 7. I get an error when I try to convert a shapefile to MySQL database using gdal-ogr ogr2ogr.exe.

This is the command I’m trying:

C:ms4wtoolsgdal-ogr>ogr2ogr -f MySQL MySQL:banyuwangigis,user=root,password=
-lco GEOMETRY_NAME=layerkecamatan c:ms4wappsbanyuwangigispetadasarshpadmin
polygon.shp

This is the resulting error:

MySQL driver does not support data source creation.

What have I missed?

Gdal image georeferencing and gdal2tiles image flip question

$
0
0

I’m georeferencing an image using gdal and the following command:

gdal_translate -of Gtiff -co tfw=yes -a_ullr 0.016264915466308594 51.505109712517786 -0.0024890899658203125 51.492018739579336 -a_srs EPSG:4326 Penguins.jpg penguins.tif

then I’m generating the tiles using gdal2tiles and the following command:

python gdal2tiles.py -t 'Penguins' -k penguins.tif penguins_gdal

but for some reason the resulting tiles show the image flipped on the y axis even though the tif image is not flipped.

Am I doing something wrong?

Thanks

Why are my raster mean and standard deviation -1?

$
0
0

I have a set of about 25 rasters whose descriptive statistics I have been examining. The data in the rasters were calculated in Python and the rasters were created using Python-GDAL.

In all but one raster, the mean, std. dev., minimum and maximum seem correct. In one raster, however, the minimum and maximum values are correct (4.842 and 9.458 respectively) but the mean and standard deviation are -1 if I take a look in ArcCatalog and -1.#IND in QGIS.

I opened the raster in Python via GDAL and calculated the minimum and maximum using data[data>band.GetNoDataValue()].min() and data[data>band.GetNoDataValue()].max() respectively, and the values these functions returned were correct (as above). But the mean value via data[data>band.GetNoDataValue()].mean() was 2.905, which is clearly nonsense as this value does not occur within the minimum and maximum value limits for this raster. Any ideas as to what’s going wrong?

Offset shapefile to start from prime meridian

$
0
0

I have Natural Earth shapefile (for example this file: link) covering whole globe in -180:180 range.
I want to offset this file, so that it starts from Greenwich prime meridian instead -180.

How can I do this with gdal/ogr?

problem with WFS Data, GDAL, Python, ArcGIS, & Defining Projection

$
0
0

I’m pulling in data in gml2 format with a EPSG#4267 from a wfs.

I create a datastream in python and then run the following from the python window in ArcMap:

data = datastream.read()

#write the gml data to a gml file
rasterdata = open("E:\Melo\USGS" + "\Roads.gml", 'w')
rasterdata.write(data)
rasterdata.close()

#Run the ogr2ogr to convert to shp
cmd = ["ogr2ogr", "-f", "ESRI Shapefile", "E:\Roads.shp", "E:\Roads.gml"]
subprocess.call(cmd)

#Define the projection
infc ="E:\Roads.shp"
prjfile = "E:\4267.prj"
arcpy.DefineProjection_management(infc, prjfile)

Unfortunately, when i try to add the layer in ArcMap 10.1 it “appears” blank. Typically, I’ve only seen this happen when their was something wrong with the projection. I know there is data in the file because I’ve converted it successfully using alternate methods.

I installd GDAL according to these instructions exactly (1500/32 bit/2.7).

I did NOT download and install the ArcGIS bindings. Do I need them?

Any ideas on what could be causing the layer to appear blank in ArcMap?


How to access DN numbers of a geotiff image using GDAL

$
0
0

Suppose that I have used this code to open a geotiff image:

#include "stdafx.h"
#include "gdal_priv.h"
#include "cpl_conv.h"
#include <vector>

using std::vector;


int _tmain()
{
    int i;
    const char* filename = "D:\JobList\Data\Image.tif";
    GDALDataset  *poDataset = NULL ;

        GDALAllRegister();

        poDataset = (GDALDataset *) GDALOpen( filename, GA_ReadOnly );
}  

Now I want to write a program that applies gaussian filter on a band of the geotiff.
the question is how can I use poDataset to extract DN numbers of a specific band?

Best way to visualize the PostGIS Raster in OpenLayers

$
0
0

I have been stuck by this problem for a long time. Several ways have already been tried, but none of them works as I was expecting. Here are the description of each of these method as well as the problem I am facing.

I want to know which is the best option to follow on because I am getting confused now.

Thanks.

  • Method 1: (GDAL + OpenLayers.Layer.Image);

    Convert the PostGIS raster to a tif file first using gdal_translate, and then translate this tif file to image (PNG) file. (Because it is faster than convert it directly from PostGIS to PNG).

    Then, load this image to Openlayers.

  • Problems: Don’t know how to make it colorful. Because the default iamge is in gray color. Don’t know how to add a color table to this. Real ResultExpected Result

  • Method 2: (MapServer + OpenLayers.Layer.WMS):
    Already get it work in a visual server with 2GB ram. But the problem is the speed. It takes long time to load the data on the map compared with load it as a image file.

    Is that the problem with our virtual server or how the way we set up the MapServer?

  • Method 3: GeoServer + Openlayers.Layer.WMS:

    Cannot get it work yet. Because I don’t know how to connect it with PostGIS raster. On the previous Post, it seems we have to connect it to JDBC. But I didn’t know someone has ever made it work or not.

Waiting for the help. Thanks.

GDAL: Geotransform – rotated rasters (and ArcGis)

$
0
0

I am working on a GDAL driver for a proprietary grid-format. In some cases, the grids are rotated in order to get smaller file sizes. In my GDAL geotransform array, I do the following (showing a hardcoded 30 degrees angle):

rot_deg = 30;
rotation = rot_deg * deg2rad;
poDS->dGeoTransform[0] = originX;
poDS->dGeoTransform[1] = cos(rotation) * cellSizeX;
poDS->dGeoTransform[2] = -sin(rotation) * cellSizeX;
poDS->dGeoTransform[3] = originY;
poDS->dGeoTransform[4] = sin(rotation) * cellSizeY;
poDS->dGeoTransform[5] = cos(rotation) * cellSizeY;

where originX and originY is the center of the corner cell.

In addition, I got an identical file in TIFF format, with same min/max + cell values.I then apply an ArcGis geotransform to the .tiff, rotating it 30 degrees aswell.

When adding both files as layers, the tiff is 100% correctly aligned to the pivot point, while my GDAL format is slightly displaced.

The screenshot shows the overlapping grids, with only the corner pixel visible. The cyan point is the pivot point. The dark pixel on top is the (correct) tiff, while the grey pixel beneath is from my own driver. The misalignment is the same over the whole grid.

Anybody got an idea what could be wrong ?

enter image description here

EDIT/update: In the ArcGis geotransform, it is possible to enter a pivot point, which is the case here (center of the corner cell). However, it seems like GDAL’s geotransform is not expressive enough (?) to handle this, hence it uses the lower left corner.

gdal_calc.py outputs huge files

$
0
0

I think this is a general enough question to apply to more than just gdal_calc, but perhaps not. When I run

gdal_calc.py -A map.tif --outfile=deforestation_00-10.tif --NoDataValue=0 --calc="A >= 7"

on a 38000 x 38000 62.5MB tif, the output is 1.37GB (and still 38000 x 38000). I feel like i’m probably missing something big here, i.e. how tif data is stored. [the same thing happens when I run it using the raster calculator in qGIS] Thanks in advance.

Bonus points–anyone know how to include and/or logic in the –calc=”…” in order to evaluate the following: (map@1 >= 7 AND map@1 <= 9) OR ((map@1 >= 10 AND map@1 <= 12) * 2)

How to sort features in a layer using OGR/Python?

$
0
0

How can I sort the features of an ogr layer by the values of a field?
I tried naive variations of

def sortlayer(ds, layer, field):
    lname = layer.GetName()
    lsort = ds.ExecuteSQL(b'select * from "{}" order by {}'.format(lname, field))
    ds.DeleteLayer(lname)
    ds.CopyLayer(lsort, lname)

but this either crashed or freezed.

Bonus points for a solution that doesn’t change the order of layers in the datasource and/or that doesn’t rely on unique layer names.

Actually the datasources are in-memory (memory-driver), and I may have up to 10 layers with up to 10.000 features over all.

Maybe it would be better not to use SQL but create an index as list, and then swap features around in the layer with Layer.GetFeature()/.SetFeature()?

Thanks, Redoute

Viewing all 397 articles
Browse latest View live