void RasterCoverage::georeference(const IGeoReference &grf, bool resetData) { if ( isReadOnly()) return; changed(true); _georef = grf; if ( resetData) _grid.reset(0); else if ( !_grid || grf->size().twod() != _grid->size().twod() ) { _grid.reset(0); } if ( _georef.isValid() ) { _georef->compute(); coordinateSystem(grf->coordinateSystem()); // mandatory resourceRef().addProperty("coordinatesystem",coordinateSystem()->id()); resourceRef().addProperty("georeference",_georef->id()); if ( coordinateSystem()->envelope(true).isValid()) resourceRef().addProperty("latlonenvelop", coordinateSystem()->envelope(true).toString()); if ( _size.isValid() && !_size.isNull() && !resetData) _size = Size<>(_georef->size().xsize(), _georef->size().ysize(), _size.zsize()); else _size = _georef->size(); Envelope env = grf->envelope(); if ( env.isValid() && !env.isNull()) envelope(grf->envelope()); } else _size = Size<>(); if (!_grid && _size.isValid()){ gridRef()->prepare(this,_size); } resourceRef().dimensions(_size.toString()); }
bool RasterCoverageConnector::loadMetaData(IlwisObject *data, const IOOptions &options){ if(!CoverageConnector::loadMetaData(data, options)) return false; auto *raster = static_cast<RasterCoverage *>(data); if (_handle->type() == GdalHandle::etGDALDatasetH){ Coordinate cMin, cMax; quint32 layer = sourceRef().hasProperty("bandindex")? sourceRef()["bandindex"].toUInt(): iUNDEF; Size<> rastersize(gdal()->xsize(_handle->handle()), gdal()->ysize(_handle->handle()), layer != iUNDEF ? 1 : gdal()->layerCount(_handle->handle())); _offsetScales.resize(rastersize.zsize()); std::vector<double> bands(layer != iUNDEF ? 1 : rastersize.zsize()); for(int i =0; i < rastersize.zsize(); ++i){ bands[i] = i; } raster->stackDefinitionRef().setSubDefinition(IDomain("count"),bands); auto layerHandle = gdal()->getRasterBand(_handle->handle(), layer != iUNDEF ? layer + 1 : 1); GDALColorInterp colorType = gdal()->colorInterpretation(layerHandle); bool ok = false; if ( layer != iUNDEF){ raster->size(rastersize); if (sourceRef().hasProperty("scale") && sourceRef().hasProperty("offset")) { _offsetScales[layer].offset = sourceRef()["offset"].toDouble(); _offsetScales[layer].scale = sourceRef()["scale"].toDouble(); } ok = handleNumericLayerCase(layer, raster); }else if( colorType <=1 || layer != iUNDEF){ // no colors + grayscale which equals image domain raster->size(rastersize); ok = handleNumericCase(raster->size(), raster); } else if ( colorType >= 3 && colorType <= 12){ raster->size(Size<>(rastersize.xsize(), rastersize.ysize(), layer != iUNDEF ? 1 : rastersize.zsize() / 3)); ok = handleColorCase(raster->size(), raster,colorType); }else if (colorType == 2){ // color palette ok = handlePaletteCase(rastersize, raster); } if (!ok) return false; IGeoReference georeference; double geosys[6]; CPLErr err = gdal()->getGeotransform(_handle->handle(), geosys) ; if ( err == CE_None) { double a1 = geosys[0]; double b1 = geosys[3]; double a2 = geosys[1]; double b2 = geosys[5]; Coordinate crdLeftup( a1 , b1); Coordinate crdRightDown(a1 + rastersize.xsize() * a2, b1 + rastersize.ysize() * b2 ) ; cMin = Coordinate( min(crdLeftup.x, crdRightDown.x), min(crdLeftup.y, crdRightDown.y)); cMax = Coordinate( max(crdLeftup.x, crdRightDown.x), max(crdLeftup.y, crdRightDown.y)); if(!georeference.prepare(_resource.url().toString())) return ERROR2(ERR_COULDNT_CREATE_OBJECT_FOR_2,"Georeference",raster->name() ); } else { int iNrTiePoints = gdal()->getGCPCount(_handle->handle()); if (iNrTiePoints > 0) { const GDAL_GCP* amtp = gdal()->getGCPs(_handle->handle()); Envelope envTieLimits; for (int i = 0; i < iNrTiePoints; i++) { Coordinate crdtiep(amtp[i].dfGCPX,amtp[i].dfGCPY,0); envTieLimits += crdtiep; } cMin = envTieLimits.min_corner(); cMax = envTieLimits.max_corner(); if(!georeference.prepare(_resource.url().toString())) return ERROR2(ERR_COULDNT_CREATE_OBJECT_FOR_2,"Georeference",raster->name() ); } else { cMin = Coordinate( 0, 0 ); cMax = Coordinate( rastersize.xsize() - 1, rastersize.ysize() - 1); if(!georeference.prepare("code=georef:undetermined")) return ERROR2(ERR_COULDNT_CREATE_OBJECT_FOR_2,"Georeference",raster->name() ); georeference->coordinateSystem(raster->coordinateSystem()); // the grf.prepare() for internal ilwis georeferences (among others "undetermined") does not autmatically set its csy georeference->envelope(Envelope(cMin, cMax)); } } georeference->size(rastersize); georeference->compute(); raster->envelope(Envelope(cMin, cMax)); raster->coordinateSystem()->envelope(raster->envelope()); raster->georeference(georeference); //if (!raster->gridRef()->prepare(raster,rastersize)) // return false; return true; }else{ return ERROR2(ERR_INVALID_PROPERTY_FOR_2,"non-RasterCoverage",_fileUrl.toLocalFile()); } }