bool TeImportRaster (TeLayer* layer, TeRaster* rasterIn, 
					 unsigned int bWidth, unsigned int bHeight,TeRasterParams::TeRasterCompressionMode compress,
					 const string& objectId, double dummy, bool useDummy,
					 TeRasterParams::TeRasterTilingType indext)
{	

	if (!layer || !rasterIn)
		return false;

	string objId;
	if (objectId.empty())
		objId = "O1";
	else
		objId = objectId;


	// check if there  is a raster geometry to be modified
	string tableGeo;
	TeRepresentation* repp = 0;
	repp = layer->getRepresentation(TeRASTER);
	if (!repp || !layer->raster(objId))	// layer doesn´t have any 
	{										// or the desired raster geometry
											// a new one should be created
		TeProjection* projIn = rasterIn->projection();	
		TeProjection* projOut = layer->projection();

		TeRasterParams parOut = rasterIn->params();		// parameters of the raster being created	
														// start with the same parameters as input raster
		
		parOut.setCompressionMode(compress);		// overwrites some parameters 
		parOut.nTilesInMemory_ = 0;					// according to the specified in the interface
		parOut.blockHeight_ = bHeight;
		parOut.blockWidth_ = bWidth;
		parOut.setDummy(dummy);
		parOut.useDummy_ = useDummy;
		parOut.tiling_type_ = indext;

		TeBox newBox = rasterIn->params().boundingBox();

		// if input raster has some rotation associated it has to be corrected 
		// during the importing
		if (parOut.dxI_ != 0. || parOut.dyJ_ != 0.)
		{
			parOut.boundingBoxResolution(newBox.x1_,newBox.y1_,newBox.x2_,newBox.y2_,parOut.resx_,parOut.resy_);
			parOut.dxI_= 0.0;							  
			parOut.dyJ_= 0.0;							 
			parOut.dxJ_ = 0.0;
			parOut.dyI_ = 0.0;	
		}
		
		bool diffProj = false;
		parOut.projection(projOut);
		if (projIn && projOut && !(*projIn == *projOut))
		{
			diffProj = true;
			TeBox boxIn = rasterIn->params().boundingBox();
			newBox = TeRemapBox (boxIn,projIn,projOut);
			parOut.resx_ = newBox.width()/parOut.ncols_;	// recalculates resolutions 
			parOut.resy_ = newBox.height()/parOut.nlines_;  // for the new projection
			parOut.boundingBoxResolution(newBox.x1_,newBox.y1_,newBox.x2_,newBox.y2_,parOut.resx_,parOut.resy_);
			newBox = parOut.box();
		}
	
		if (indext == TeRasterParams::TeExpansible)			// if mosaicable adjust box 
			parOut.resizeToTiling(newBox,bWidth,bHeight);	// to be cut in blocks of bWidth X bHeight (in projection units)

		parOut.decoderIdentifier_ = "DB";					// parameters of the decoder
		parOut.fileName_ = "RasterLayer" + Te2String(layer->id()) + "_R_" + objId;

		if (parOut.photometric_[0] == TeRasterParams::TePallete)
			parOut.lutName_ = parOut.fileName_ + "_LUT";

		if (!layer->addRasterGeometry(parOut,objId))		// creates the empty raster geometry
			return false;

		// create a decoder to raster in a TerraLib database 
		parOut.mode_ = 'w';
		TeDecoderDatabase *dbDecoder = new TeDecoderDatabase(parOut);
		dbDecoder->setDB(layer->database());

		// optimization: we don't need to go through remap routines.
		// just cut the input raster and save the tiles
		bool res=false;
		TeRasterParams parF;
		if (indext == TeRasterParams::TeNoExpansible && !diffProj)
		{
			int ntiles = (parOut.nlines_/parOut.blockHeight_)*(parOut.ncols_/parOut.blockWidth_);
			if(TeProgress::instance())
				TeProgress::instance()->setTotalSteps(ntiles);
			int count = 0;
			clock_t	ti, tf;
			int x=0, y=0, b, nb=parOut.nBands(),lx, ly;
			double val= parOut.dummy_[0];
			TeMemoryPage page(parOut.blockHeight_*parOut.blockWidth_,val,parOut.dataType_[0]);
			string index;
			ti = clock();
			while (y<parOut.nlines_)  // for each row of tiles
			{
				page.ulLin_ = y;
				x=0;
				while (x<parOut.ncols_)	// for each tile in a row
				{
					page.clear();
					page.ulCol_= x;
					for (b=0; b<nb; ++b)    // for each band;
					{
						for (ly=0; ly<parOut.blockHeight_; ++ly)
						{
							for (lx=0; lx<parOut.blockWidth_; ++lx)  // write the tile
							{
								if (rasterIn->getElement(x+lx,y+ly,val,b))
								{
									page.setVal(x+lx,y+ly,parOut.blockWidth_,val);
									if (val<parOut.vmin_[b])
										parOut.vmin_[b] = val;
									if (val>parOut.vmax_[b])
										parOut.vmax_[b] = val;
								}
							}
						}
						index = dbDecoder->codifyId(x,y,b,1,0);
						if (!dbDecoder->putRasterBlock(index,page.data_,page.pageSize()))
							res=false;
						page.clear();
					}
					x+= parOut.blockWidth_;
					++count;
					if(TeProgress::instance())
					{
						tf = clock();
						if (int((tf-ti)/CLOCKS_PER_SEC) > 3)
						{
							if (TeProgress::instance()->wasCancelled())
								break;
							TeProgress::instance()->setProgress(count);
							ti = tf;
						}
					}
				}
				y+= parOut.blockHeight_;
			}
			if (TeProgress::instance())
				TeProgress::instance()->reset();

			// atualizes the representation box in memory
			TeRepresentation* repp = layer->getRepresentation(TeRASTER);
			updateBox(repp->box_,parOut.box());
			repp->nCols_ = parOut.ncols_;
			repp->nLins_ = parOut.nlines_;
			repp->resX_ = parOut.resx_;
			repp->resY_ = parOut.resy_;
			layer->updateLayerBox(parOut.boundingBox());
			layer->database()->updateRasterRepresentation(layer->id(),dbDecoder->params(),objId);
			// atualizes representation in the database
			for (b=0; b<nb; ++b)
			{
				dbDecoder->params().vmax_[b]=parOut.vmax_[b];
				dbDecoder->params().vmin_[b]=parOut.vmin_[b];
			}
			layer->database()->updateRasterRepresentation(layer->id(),dbDecoder->params(),objId);
			dbDecoder->clear();
			res=true;
		}
		else
		{
			TeRaster* rasterOut = new TeRaster();
			rasterOut->setDecoder(dbDecoder);
			rasterOut->init();
			TeRasterRemap remap(rasterIn, rasterOut);
			if (remap.apply(true))
			{
				TeRepresentation* repp = layer->getRepresentation(TeRASTER);
				updateBox(repp->box_,rasterOut->params().box());
				repp->nCols_ = rasterOut->params().ncols_;
				repp->nLins_ = rasterOut->params().nlines_;
				repp->resX_ = rasterOut->params().resx_;
				repp->resY_ = rasterOut->params().resy_;

				// atualizes representation in the database
				layer->database()->updateRasterRepresentation(layer->id(),rasterOut->params(),objId);
				layer->updateLayerBox(rasterOut->params().boundingBox());
				rasterOut->clear();
				res = true;
			}
		}
		if (res)			
		{
			//	create spatial index in the block box
			string tableR = layer->database()->getRasterTable(layer->id(),objId);
			layer->database()->insertMetadata(tableR,layer->database()->getSpatialIdxColumn(TeRASTER), 0.000005,0.000005,layer->box());		
			layer->database()->createSpatialIndex(tableR,layer->database()->getSpatialIdxColumn(TeRASTER), (TeSpatialIndexType)TeRTREE);
		}
		delete dbDecoder;
		return res;
	}
	else
	{
		// layer contains already a raster representation associated to the
		// object id: calls mosaic operation
		return TeMosaicRaster(rasterIn,layer,objId);
	}
}
// This function mosaics an input raster to one previsously existing in a TerraLib layer
bool TeMosaicRaster(TeRaster* rasterIn, TeLayer* layer,  const string& objectId)
{

	// layer must exist and input raster should be read to be read
	if (!layer || !rasterIn  || rasterIn->params().status_ != TeRasterParams::TeReadyToRead)
		return false;
	
	TeRaster* rasterOut;
	if (!objectId.empty())	// if object id informed try to get raster associated to it
		rasterOut = layer->raster(objectId,'w');
	else					// else try to get first one found
		rasterOut = layer->raster();

	if (!rasterOut)			// no raster asked or no raster at all
		return false;				// can not do mosaic

	if (rasterOut->params().tiling_type_==TeRasterParams::TeNoExpansible)
		return false;				// can mosaic if is not possible

	// allows mosaic of same photometric types only
	if (rasterOut->params().photometric_[0] != rasterIn->params().photometric_[0])
		return false;

	// increases output box to: input raster box + output raster box 
  TeBox boxIn = rasterIn->params().boundingBox();

	// if necessary remap input box to output projection 
	if (!(*(rasterIn->projection()) == *(rasterOut->projection())))
		boxIn = TeRemapBox(boxIn,rasterIn->projection(),rasterOut->projection());

	// sum stored and input boxes
	TeBox newBoxa = rasterOut->params().boundingBox();
	updateBox(newBoxa,boxIn);	
  
  // adjust it to expansible values
	TeBox newBox = adjustToCut(newBoxa, rasterOut->params().blockWidth_*rasterOut->params().resx_, 
							   rasterOut->params().blockHeight_*rasterOut->params().resy_);

	// update box in parameters of the raster and it's decoder
	rasterOut->params().boundingBoxResolution(newBox.x1_,newBox.y1_,newBox.x2_,newBox.y2_,
											  rasterOut->params().resx_, rasterOut->params().resy_);

	rasterOut->decoder()->params().boundingBoxResolution(newBox.x1_,newBox.y1_,newBox.x2_,newBox.y2_,
											  rasterOut->params().resx_, rasterOut->params().resy_);
	rasterOut->params().nTilesInMemory_ = 0;
	
	// creates a remapping from input raster to output raster
	TeRasterRemap remap(rasterIn, rasterOut);
	if (remap.apply(true))			// o.k.
	{
		TeRasterParams rp = rasterOut->params();

		// atualizes the representation box in memory
		TeRepresentation* repp = layer->getRepresentation(TeRASTER);
		TeBox boxNew = rp.box();
		updateBox(repp->box_,rp.box());
		repp->nCols_ = rp.ncols_;
		repp->nLins_ = rp.nlines_;
		repp->resX_ = rp.resx_;
		repp->resY_ = rp.resy_;

		// atualizes representation in the database
		layer->database()->updateRasterRepresentation(layer->id(),rp,objectId);
		layer->updateLayerBox(rp.boundingBox());
		rasterOut->clear();
		return true;
	}
	return false;
}
Пример #3
0
void VoronoiWindow::okPushButton_clicked()
{
    std::string layerName = layerNameLineEdit->text().utf8();	
	if(layerName.empty())
	{
		QMessageBox::information(this, tr("Information"), tr("Please, define a name to result Layer."));
        voronoiTabWidget->setCurrentPage(0);
        layerNameLineEdit->setFocus();
		return;
	}

    std::string layerLinesName;
    if(generateLinesCheckBox->isChecked())
    {
        layerLinesName = layerLinesLineEdit->text().ascii();
        if(layerLinesName.empty())
	    {
		    QMessageBox::information(this, tr("Information"), tr("Please, define a name to Layer of Lines."));
            voronoiTabWidget->setCurrentPage(1);
            layerLinesLineEdit->setFocus();
		    return;
	    }
    }

    if(!isLayerNameValid(layerName))
    {
        voronoiTabWidget->setCurrentPage(0);
        layerNameLineEdit->setFocus();
        return;
    }

    if(!isLayerNameValid(layerLinesName))
    {
        voronoiTabWidget->setCurrentPage(1);
        layerLinesLineEdit->setFocus();
        return;
    }

    if(layerName == layerLinesName)
    {
        QMessageBox::information(this, tr("Information"), tr("Please, define names differents to Layer result and Layer of Lines."));
	    return;
    }

	TeDatabase* db = plugin_params_->getCurrentDatabasePtr();
    TeTheme* theme = getTheme(themeComboBox->currentText().latin1());
	if(theme == 0)
	{
		QMessageBox::critical(this, tr("Error"), tr("Error getting the input Theme."));
		return;
	}
    
    TePrecision::instance().setPrecision(TeGetPrecision(theme->layer()->projection()));
    
    TeTheme *themeDelimiter = getTheme(boxComboBox->currentText().latin1());
    if(themeDelimiter == 0)
    {
        QMessageBox::critical(this, tr("Error"), tr("Error getting the delimiter Layer."));
		return;
    }

    // Verifies is the box chosen is valid
    TeBox b = themeDelimiter->layer()->box();
    TeProjection* projFrom = themeDelimiter->layer()->projection();
    TeProjection* projTo = theme->layer()->projection();
    if(!((*projFrom) == (*projTo))) // need remap?
        b = TeRemapBox(b, projFrom, projTo);

    TeBox& inputBox = theme->layer()->box();
    if(!TeIntersects(b, inputBox))
    {
        QMessageBox::information(this, tr("Information"), tr("The box chosen do not intercepts the input Theme. Please, try another."));
        voronoiTabWidget->setCurrentPage(1);
        boxComboBox->setFocus();
        return;
    }
    
    //preparing to read the layer
    bool loadAllAttributes=false;
    if (diagramType==MWVoronoi) loadAllAttributes = true;
    bool loadGeometries = true;
    TeQuerierParams querierParams(loadGeometries, loadAllAttributes);
    querierParams.setParams(theme->layer());
    TeQuerier  querier(querierParams);
    querier.loadInstances();
    //finding weight's attribute
    TeAttributeList attrList = querier.getAttrList();
    int weightAttrN=0;
    if (diagramType==MWVoronoi) {
        for (std::vector<TeAttribute>::iterator i=attrList.begin(); i!=attrList.end();++i) {
            if (weightComboBox->currentText()==i->rep_.name_) {
                break;
            }
            weightAttrN++;
        }    
    }
    int n=querier.numElemInstances();

    if(n==0)
    {
        QMessageBox::critical(this, tr("Error"), tr("Error getting the points of input Theme."));
        return;
    }

    TeWaitCursor wait;

    // Converts x,y to a float array in order to pass to VoronoiDiagramGenerator class
    float* x = new float[n];
	float* y = new float[n];
    //pointers for weighted voronoi
    float* w;
    int numPoints;
    w= new float[n];
    numPoints = 0;
    TeSTInstance sti;
    string peso;
    //TePoint pointBefore(0,0);
    while(querier.fetchInstance(sti)) { // for each point
        // Stores on float array
        if(sti.hasPoints())
            {
                TePointSet pointSet;
                //reading geometry
                sti.getGeometry(pointSet);
                x[numPoints] = pointSet[0].location().x();
                y[numPoints] = pointSet[0].location().y();
    
                //reading weight
                if (diagramType==MWVoronoi) {
                    stringstream ss;
                    sti.getPropertyValue(peso,weightAttrN);
                    ss<<peso;
                    ss>>w[numPoints];
                }                    
                numPoints++;
            }