Пример #1
0
bool wxGISDrawingLayer::Draw(wxGISEnumDrawPhase DrawPhase, ITrackCancel* const pTrackCancel)
{
    wxCHECK_MSG(m_pDisplay, false, wxT("Display pointer is NULL"));

    wxCriticalSectionLocker lock(m_CritSect);

    OGREnvelope stDisplayExtentRotated = m_pDisplay->GetBounds(true);
    OGREnvelope stFeatureDatasetExtentRotated = m_oLayerExtent;

    //rotate featureclass extent
    if (!IsDoubleEquil(m_pDisplay->GetRotate(), 0.0))
    {
        wxRealPoint dfCenter = m_pDisplay->GetBoundsCenter();
        RotateEnvelope(stFeatureDatasetExtentRotated, m_pDisplay->GetRotate(), dfCenter.x, dfCenter.y);//dCenterX, dCenterY);
    }

    //if envelopes don't intersect exit
    if (!stDisplayExtentRotated.Intersects(stFeatureDatasetExtentRotated))
        return false;

    //get intersect envelope to fill vector data
    OGREnvelope stDrawBounds = stDisplayExtentRotated;
    stDrawBounds.Intersect(stFeatureDatasetExtentRotated);
    if (!stDrawBounds.IsInit())
        return false;

    IProgressor* pProgress = NULL;
    if (NULL != pTrackCancel)
    {
        pProgress = pTrackCancel->GetProgressor();
    }

    if (NULL != pProgress)
    {
        pProgress->SetRange(m_aoShapes.size());
    }

    //draw shapes
    for (size_t i = 0; i < m_aoShapes.size(); ++i)
    {
        if (NULL != pProgress)
        {
            pProgress->SetValue(i);
        }

        if (pTrackCancel != NULL && !pTrackCancel->Continue())
            return true;
        if (m_aoShapes[i] != NULL)
        {
            m_aoShapes[i]->Draw(m_pDisplay);
        }
    }

    //draw enumGISShapeStateSelected

    //draw enumGISShapeStateRotated

    //draw enumGISShapeStatePoints

    return true;
}
Пример #2
0
bool SubrasterByVector(wxGISFeatureDatasetSPtr pSrcFeatureDataSet, wxGISRasterDatasetSPtr pSrcRasterDataSet, CPLString &szDstFolderPath, wxGxRasterFilter* pFilter, GDALDataType eOutputType, int nBandCount, int *panBandList, bool bUseCounter, int nCounterBegin, int nFieldNo, double dfOutResX, double dfOutResY, bool bCopyNodata, bool bSkipSourceMetadata, char** papszOptions, ITrackCancel* pTrackCancel)
{
	//check if openned or/and open dataset
	if(!pSrcFeatureDataSet->IsOpened())
	{
		if(!pSrcFeatureDataSet->Open(true))
		{
			if(pTrackCancel)
				pTrackCancel->PutMessage(_("Source vector dataset open failed"), -1, enumGISMessageErr);
			return false;
		}
	}

	if(!pSrcRasterDataSet->IsOpened())
	{
		if(!pSrcRasterDataSet->Open(true))
		{
			if(pTrackCancel)
				pTrackCancel->PutMessage(_("Source raster dataset open failed"), -1, enumGISMessageErr);
			return false;
		}
	}

    const OGRSpatialReferenceSPtr pSrsSRS = pSrcFeatureDataSet->GetSpatialReference();
    const OGRSpatialReferenceSPtr pDstSRS = pSrcRasterDataSet->GetSpatialReference();
    OGRCoordinateTransformation *poCT(NULL);
    bool bSame = pSrsSRS == NULL || pDstSRS == NULL || pSrsSRS->IsSame(pDstSRS.get());
    if( !bSame )
	{
        poCT = OGRCreateCoordinateTransformation( pSrsSRS.get(), pDstSRS.get() );
		if(poCT == NULL)
		{
		    const char* err = CPLGetLastErrorMsg();
			wxString sWarn = wxString::Format(_("Create OGRCreateCoordinateTransformation failed! GDAL error: %s"), wxString(err, wxConvUTF8).c_str());
            wxLogWarning(sWarn);
			if(pTrackCancel)
				pTrackCancel->PutMessage(sWarn, -1, enumGISMessageWarning);
		}
	}

    IProgressor* pProgressor(NULL);
    if(pTrackCancel)
    {
       pProgressor = pTrackCancel->GetProgressor();
       pTrackCancel->PutMessage(wxString::Format(_("Start clip '%s' by geometry from '%s'"), wxString(pSrcRasterDataSet->GetPath(), wxConvUTF8).c_str(), wxString(pSrcFeatureDataSet->GetPath(), wxConvUTF8).c_str()), -1, enumGISMessageNorm);
    }
    int nCounter(0);
    if(pProgressor)
        pProgressor->SetRange(pSrcFeatureDataSet->GetFeatureCount());


/* -------------------------------------------------------------------- */
/*	Build band list to translate					                    */
/* -------------------------------------------------------------------- */
    if( nBandCount == 0 )
    {
        nBandCount = pSrcRasterDataSet->GetBandCount();
        if( nBandCount == 0 )
        {
		    if(pTrackCancel)
			    pTrackCancel->PutMessage(_("Input file has no bands, and so cannot be translated."), -1, enumGISMessageErr);
            return false;
        }

        panBandList = (int *) CPLMalloc(sizeof(int)*nBandCount);
        for(size_t i = 0; i < nBandCount; ++i )
            panBandList[i] = i + 1;
    }
    else
    {
        for(size_t i = 0; i < nBandCount; ++i )
        {
            if( panBandList[i] > pSrcRasterDataSet->GetBandCount() )
            {
		        if(pTrackCancel)
                    pTrackCancel->PutMessage(wxString::Format(_("Band %d requested, but only bands 1 to %d available."), panBandList[i], pSrcRasterDataSet->GetBandCount()), -1, enumGISMessageErr);
                return false;
            }
        }
    }

    bool bDefaultfilter(false);
    if(pFilter == NULL)
    {
        pFilter = new wxGxRasterFilter(enumRasterTiff);
        bDefaultfilter = true;
    }

    CPLString szDriver(pFilter->GetDriver().mb_str());
    CPLString szExt(pFilter->GetExt().mb_str());
    CPLString szBaseName = CPLGetBasename(pSrcRasterDataSet->GetPath());

	GDALDriver* pDriver = (GDALDriver*)GDALGetDriverByName( szDriver );
	if( pDriver == NULL )
    {
		if(pTrackCancel)
            pTrackCancel->PutMessage(wxString::Format(_("Output driver '%s' not recognised."), szDriver.c_str()), -1, enumGISMessageErr);
        if(bDefaultfilter)
            wxDELETE(pFilter);
		return false;
    }

    pSrcFeatureDataSet->Reset();
    OGRFeatureSPtr pFeature;
    size_t nNameCounter(nCounterBegin);
    while((pFeature = pSrcFeatureDataSet->Next()) != NULL)
    {
        if(pTrackCancel && !pTrackCancel->Continue())
        {
            wxString sErr(_("Interrupted by user"));
            CPLString sFullErr(sErr.mb_str());
            CPLError( CE_Warning, CPLE_AppDefined, sFullErr );

            if(pTrackCancel)
                pTrackCancel->PutMessage(wxString(sFullErr, wxConvLocal), -1, enumGISMessageErr);

            if(bDefaultfilter)
                wxDELETE(pFilter);
            return false;
        }

        OGRGeometry *pGeom = pFeature->GetGeometryRef();
		if(wkbFlatten(pSrcFeatureDataSet->GetGeometryType()) != wkbUnknown && !pGeom)
            continue;

		OGRGeometry *pNewGeom(NULL);
        if( !bSame && poCT )
        {
            if(pGeom)
            {
				pNewGeom = pGeom->clone();
                OGRErr eErr = pNewGeom->transform(poCT);
                if(eErr != OGRERR_NONE)
	                wxDELETE(pNewGeom);
            }
        }
		else
			pNewGeom = pGeom->clone();

		OGREnvelope GeomEnv;
		pNewGeom->getEnvelope(&GeomEnv);
		OGREnvelope RasterEnv = pSrcRasterDataSet->GetEnvelope();
		GeomEnv.Intersect(RasterEnv);
		if(GeomEnv.IsInit())
		{
            CPLString szPath;
			if(bUseCounter)
            {
                szPath.Printf("%s_%d", szBaseName.c_str(), nNameCounter++);
                CPLString sNewName(CheckUniqName(szDstFolderPath, szPath, szExt).mb_str(wxConvUTF8));
                szPath = CPLFormFilename(szDstFolderPath, sNewName, szExt);
            }
            else
            {
                CPLString szName = pFeature->GetFieldAsString(nFieldNo);
                CPLString sNewName(CheckUniqName(szDstFolderPath, szName, szExt).mb_str(wxConvUTF8));
                szPath = CPLFormFilename(szDstFolderPath, sNewName, szExt);
            }
			CreateSubRaster(pSrcRasterDataSet, GeomEnv, pNewGeom, pDriver, szPath, eOutputType, nBandCount, panBandList, dfOutResX, dfOutResY, bCopyNodata, bSkipSourceMetadata, papszOptions, pTrackCancel);
		}
        nCounter++;
        if(pProgressor)
            pProgressor->SetValue(nCounter);
    }

    if(poCT)
        OCTDestroyCoordinateTransformation(poCT);

    if(bDefaultfilter)
        wxDELETE(pFilter);

	return true;
}