[gdal-dev] Encoding transform data in Shapefile

Björn Piltz bjornpiltz at blikken.de
Tue Mar 3 05:38:47 PST 2015


Hi,
I'm extracting contours in geo-referenced images and saving the results to
shape files. At a later point I would like to read the shapefile and
transform the contours into the original integer coordinate system of the
original image. To be able to do this, I would have to save the "geo
transform" (or the inverse) in the shape file. Is there any way I can do
this?

Here is my current save routine(minus error checking etc)

/*
projection = PROJCS["WGS_1984_UTM_Zone_...
geoTransform = [0.1   0  x]
               [  0  -0.1 y]
               */
bool OGRShapeFile::write(const PolygonIWithHolesList& imageContours, const
QString& filename, const QString& projection, const QTransform&
geoTransform)
{
    // imageContours are integer pixel coordinates
    // first we transform them according to the geo transform.
    PolygonFWithHolesList contours;
    foreach(PolygonIWithHoles contour, imageContours)
        contours << poly->toPolygonF(geoTransform);

    OGRSpatialReference spatialReference;
    if (!projection.isNull())
    {
        std::string tmp1 = projection.toStdString();
        char* tmp2 = (char*)tmp1.c_str();
        spatialReference.importFromWkt(&tmp2);
    }

    OGRSFDriver* poDriver =
OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName("ESRI Shapefile");
    OGRDataSource *poDS =
poDriver->CreateDataSource(filename.toLocal8Bit(), NULL);
    OGRLayer *poLayer = poDS->CreateLayer("point", &spatialReference,
wkbPolygon, NULL);

    foreach(const PolygonFWithHoles& polygon, contours)
    {
        OGRPolygon p;
        p.assignSpatialReference(&spatialReference);
        for (int i=0; i<polygon.holeCount()+1; i++)
        {
            ...
            p.addRing(&ring);
        }

        OGRFeature *poFeature =
OGRFeature::CreateFeature(poLayer->GetLayerDefn());
        poFeature->SetGeometry(&p);
        poLayer->CreateFeature(poFeature);
        OGRFeature::DestroyFeature(poFeature);
    }
    OGRDataSource::DestroyDataSource(poDS);
    return true;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20150303/967bc0c3/attachment.html>


More information about the gdal-dev mailing list