CPLErr GDALGridInverseDistanceToAPowerNoSearch(const void *poOptions, GUInt32 nPoints, const double *padfX, const double *padfY, const double *padfZ, double dfXPoint, double dfYPoint, double *pdfValue) { const double dfPower = ((GDALGridInverseDistanceToAPowerOptions*)poOptions)->dfPower; const double dfSmoothing = ((GDALGridInverseDistanceToAPowerOptions*)poOptions)->dfSmoothing; double dfNominator = 0.0, dfDenominator = 0.0; GUInt32 i; for (i = 0; i < nPoints; i++) { const double dfRX = padfX[i] - dfXPoint; const double dfRY = padfY[i] - dfYPoint; const double dfR2 = dfRX * dfRX + dfRY * dfRY + dfSmoothing * dfSmoothing; // If the test point is close to the grid node, use the point // value directly as a node value to avoid singularity. if (CPLIsEqual(dfR2, 0.0)) { (*pdfValue) = padfZ[i]; return CE_None; } else { const double dfW = pow(sqrt(dfR2), dfPower); dfNominator += padfZ[i] / dfW; dfDenominator += 1.0 / dfW; } } if (dfDenominator == 0.0) { (*pdfValue) = ((GDALGridInverseDistanceToAPowerOptions*)poOptions)->dfNoDataValue; } else (*pdfValue) = dfNominator / dfDenominator; return CE_None; }
// Load a buffer into a zImg template <typename T> void CntZImgFill(CntZImage &zImg, T *src, const ILImage &img) { int w = img.pagesize.x; int h = img.pagesize.y; zImg.resize(w, h); T *ptr = src; // No data value float ndv = float(img.NoDataValue); if (!img.hasNoData) ndv = 0; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { zImg(i, j).z = float(*ptr++); zImg(i, j).cnt = !CPLIsEqual(zImg(i, j).z, ndv); } return; }
int GDALFillBandNoData(GDALDataset *poDS, int nBand, int nSearchPixels) { (void)nBand; (void)nSearchPixels; if(poDS == NULL) { fprintf(stderr, "Invalid GDAL Dataset Handle, cannot fill no data\n"); return -1; } int nPixels, nLines; nPixels = poDS->GetRasterXSize(); nLines = poDS->GetRasterYSize(); GDALRasterBand *poBand; poBand = poDS->GetRasterBand(1); GDALFillNodata(poBand, NULL, 100, 0, 0, NULL, NULL, NULL); double dfNoData = poBand->GetNoDataValue(NULL); double *padfScanline; padfScanline = (double *) CPLMalloc(sizeof(double)*nPixels); int nNoDataCount = 0; for(int i = 0;i < nLines;i++) { GDALRasterIO(poBand, GF_Read, 0, i, nPixels, 1, padfScanline, nPixels, 1, GDT_Float64, 0, 0); for(int j = 0; j < nPixels;j++) { if(CPLIsEqual(padfScanline[j], dfNoData)) nNoDataCount++; } } CPLFree(padfScanline); return nNoDataCount; }
/** Check for no data values in a band for an image * @param poDS a pointer to a valid GDALDataset * @param band an integer representation of which band in the image * @return true if the band contains any no data values * @warning May be cpu intensive on large images */ bool GDALHasNoData( GDALDataset *poDS, int band ) { bool hasNDV = false; //check if poDS is NULL #lm int ncols = poDS->GetRasterXSize(); int nrows = poDS->GetRasterYSize(); double nDV; GDALRasterBand *poBand = poDS->GetRasterBand( band ); if( poBand == NULL ) return false; int pbSuccess = 0; nDV = poBand->GetNoDataValue( &pbSuccess ); if( pbSuccess == false ) nDV = -9999.0; double *padfScanline; padfScanline = new double[ncols]; for( int i = 0;i < nrows;i++ ) { poBand->RasterIO( GF_Read, 0, i, ncols, 1, padfScanline, ncols, 1, GDT_Float64, 0, 0 ); for( int j = 0;j < ncols;j++ ) { if( CPLIsEqual( (float)padfScanline[j], (float)nDV ) ) { hasNDV = true; goto done; } } } done: delete[] padfScanline; return hasNDV; }
OGRErr OGRSpatialReference::exportToUSGS( long *piProjSys, long *piZone, double **ppadfPrjParams, long *piDatum ) const { const char *pszProjection = GetAttrValue("PROJECTION"); /* -------------------------------------------------------------------- */ /* Fill all projection parameters with zero. */ /* -------------------------------------------------------------------- */ int i; *ppadfPrjParams = (double *)CPLMalloc( 15 * sizeof(double) ); for ( i = 0; i < 15; i++ ) (*ppadfPrjParams)[i] = 0.0; *piZone = 0L; /* ==================================================================== */ /* Handle the projection definition. */ /* ==================================================================== */ if( IsLocal() ) *piProjSys = GEO; else if( pszProjection == NULL ) { #ifdef DEBUG CPLDebug( "OSR_USGS", "Empty projection definition, considered as Geographic" ); #endif *piProjSys = GEO; } else if( EQUAL(pszProjection, SRS_PT_ALBERS_CONIC_EQUAL_AREA) ) { *piProjSys = ALBERS; (*ppadfPrjParams)[2] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_STANDARD_PARALLEL_1, 0.0 ) ); (*ppadfPrjParams)[3] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_STANDARD_PARALLEL_2, 0.0 ) ); (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP) ) { *piProjSys = LAMCC; (*ppadfPrjParams)[2] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_STANDARD_PARALLEL_1, 0.0 ) ); (*ppadfPrjParams)[3] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_STANDARD_PARALLEL_2, 0.0 ) ); (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_MERCATOR_1SP) ) { *piProjSys = MERCAT; (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_POLAR_STEREOGRAPHIC) ) { *piProjSys = PS; (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_POLYCONIC) ) { *piProjSys = POLYC; (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_EQUIDISTANT_CONIC) ) { *piProjSys = EQUIDC; (*ppadfPrjParams)[2] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_STANDARD_PARALLEL_1, 0.0 ) ); (*ppadfPrjParams)[3] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_STANDARD_PARALLEL_2, 0.0 ) ); (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); (*ppadfPrjParams)[8] = 1.0; } else if( EQUAL(pszProjection, SRS_PT_TRANSVERSE_MERCATOR) ) { int bNorth; *piZone = GetUTMZone( &bNorth ); if( *piZone != 0 ) { *piProjSys = UTM; if( !bNorth ) *piZone = - *piZone; } else { *piProjSys = TM; (*ppadfPrjParams)[2] = GetNormProjParm( SRS_PP_SCALE_FACTOR, 1.0 ); (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } } else if( EQUAL(pszProjection, SRS_PT_STEREOGRAPHIC) ) { *piProjSys = STEREO; (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_LAMBERT_AZIMUTHAL_EQUAL_AREA) ) { *piProjSys = LAMAZ; (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_AZIMUTHAL_EQUIDISTANT) ) { *piProjSys = AZMEQD; (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LONGITUDE_OF_CENTER, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_CENTER, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_GNOMONIC) ) { *piProjSys = GNOMON; (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_ORTHOGRAPHIC) ) { *piProjSys = ORTHO; (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_SINUSOIDAL) ) { *piProjSys = SNSOID; (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LONGITUDE_OF_CENTER, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_EQUIRECTANGULAR) ) { *piProjSys = EQRECT; (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_STANDARD_PARALLEL_1, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_MILLER_CYLINDRICAL) ) { *piProjSys = MILLER; (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LONGITUDE_OF_CENTER, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_CENTER, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_VANDERGRINTEN) ) { *piProjSys = VGRINT; (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LONGITUDE_OF_CENTER, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_HOTINE_OBLIQUE_MERCATOR) ) { *piProjSys = HOM; (*ppadfPrjParams)[2] = GetNormProjParm( SRS_PP_SCALE_FACTOR, 1.0 ); (*ppadfPrjParams)[3] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_AZIMUTH, 0.0 ) ); (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LONGITUDE_OF_CENTER, 0.0 ) ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_CENTER, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); (*ppadfPrjParams)[12] = 1.0; } else if( EQUAL(pszProjection, SRS_PT_HOTINE_OBLIQUE_MERCATOR_TWO_POINT_NATURAL_ORIGIN) ) { *piProjSys = HOM; (*ppadfPrjParams)[2] = GetNormProjParm( SRS_PP_SCALE_FACTOR, 1.0 ); (*ppadfPrjParams)[5] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_CENTER, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); (*ppadfPrjParams)[8] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LONGITUDE_OF_POINT_1, 0.0 ) ); (*ppadfPrjParams)[9] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_POINT_1, 0.0 ) ); (*ppadfPrjParams)[10] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LONGITUDE_OF_POINT_2, 0.0 ) ); (*ppadfPrjParams)[11] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LATITUDE_OF_POINT_2, 0.0 ) ); (*ppadfPrjParams)[12] = 0.0; } else if( EQUAL(pszProjection, SRS_PT_ROBINSON) ) { *piProjSys = ROBIN; (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_LONGITUDE_OF_CENTER, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_MOLLWEIDE) ) { *piProjSys = MOLL; (*ppadfPrjParams)[4] = CPLDecToPackedDMS( GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ) ); (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_WAGNER_IV) ) { *piProjSys = WAGIV; (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_WAGNER_VII) ) { *piProjSys = WAGVII; (*ppadfPrjParams)[6] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); (*ppadfPrjParams)[7] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } // Projection unsupported by GCTP else { CPLDebug( "OSR_USGS", "Projection \"%s\" unsupported by USGS GCTP. " "Geographic system will be used.", pszProjection ); *piProjSys = GEO; } /* -------------------------------------------------------------------- */ /* Translate the datum. */ /* -------------------------------------------------------------------- */ const char *pszDatum = GetAttrValue( "DATUM" ); if ( pszDatum ) { if( EQUAL( pszDatum, SRS_DN_NAD27 ) ) *piDatum = CLARKE1866; else if( EQUAL( pszDatum, SRS_DN_NAD83 ) ) *piDatum = GRS1980; else if( EQUAL( pszDatum, SRS_DN_WGS84 ) ) *piDatum = WGS84; // If not found well known datum, translate ellipsoid else { double dfSemiMajor = GetSemiMajor(); double dfInvFlattening = GetInvFlattening(); #ifdef DEBUG CPLDebug( "OSR_USGS", "Datum \"%s\" unsupported by USGS GCTP. " "Try to translate ellipsoid definition.", pszDatum ); #endif for ( i = 0; i < NUMBER_OF_ELLIPSOIDS; i++ ) { double dfSM; double dfIF; if ( OSRGetEllipsoidInfo( aoEllips[i], NULL, &dfSM, &dfIF ) == OGRERR_NONE && CPLIsEqual( dfSemiMajor, dfSM ) && CPLIsEqual( dfInvFlattening, dfIF ) ) { *piDatum = i; break; } } if ( i == NUMBER_OF_ELLIPSOIDS ) // Didn't found matches; set { // custom ellipsoid parameters #ifdef DEBUG CPLDebug( "OSR_USGS", "Ellipsoid \"%s\" unsupported by USGS GCTP. " "Custom ellipsoid definition will be used.", pszDatum ); #endif *piDatum = -1; (*ppadfPrjParams)[0] = dfSemiMajor; if ( ABS( dfInvFlattening ) < 0.000000000001 ) { (*ppadfPrjParams)[1] = dfSemiMajor; } else { (*ppadfPrjParams)[1] = dfSemiMajor * (1.0 - 1.0/dfInvFlattening); } } } } else *piDatum = -1; return OGRERR_NONE; }
CPLErr GDALGridInverseDistanceToAPower(const void *poOptions, GUInt32 nPoints, const double *padfX, const double *padfY, const double *padfZ, double dfXPoint, double dfYPoint, double *pdfValue) { // TODO: For optimization purposes pre-computed parameters should be moved // out of this routine to the calling function. // Pre-compute search ellipse parameters double dfRadius1 = ((GDALGridInverseDistanceToAPowerOptions*)poOptions)->dfRadius1; double dfRadius2 = ((GDALGridInverseDistanceToAPowerOptions*)poOptions)->dfRadius2; double dfR12; dfRadius1 *= dfRadius1; dfRadius2 *= dfRadius2; dfR12 = dfRadius1 * dfRadius2; // Compute coefficients for coordinate system rotation. double dfCoeff1 = 0.0, dfCoeff2 = 0.0; const double dfAngle = TO_RADIANS * ((GDALGridInverseDistanceToAPowerOptions*)poOptions)->dfAngle; const bool bRotated = (dfAngle == 0.0) ? false : true; if (bRotated) { dfCoeff1 = cos(dfAngle); dfCoeff2 = sin(dfAngle); } const double dfPower = ((GDALGridInverseDistanceToAPowerOptions*)poOptions)->dfPower; const double dfSmoothing = ((GDALGridInverseDistanceToAPowerOptions*)poOptions)->dfSmoothing; const GUInt32 nMaxPoints = ((GDALGridInverseDistanceToAPowerOptions*)poOptions)->nMaxPoints; double dfNominator = 0.0, dfDenominator = 0.0; GUInt32 i, n = 0; for (i = 0; i < nPoints; i++) { double dfRX = padfX[i] - dfXPoint; double dfRY = padfY[i] - dfYPoint; const double dfR2 = dfRX * dfRX + dfRY * dfRY + dfSmoothing * dfSmoothing; if (bRotated) { double dfRXRotated = dfRX * dfCoeff1 + dfRY * dfCoeff2; double dfRYRotated = dfRY * dfCoeff1 - dfRX * dfCoeff2; dfRX = dfRXRotated; dfRY = dfRYRotated; } // Is this point located inside the search ellipse? if (dfRadius2 * dfRX * dfRX + dfRadius1 * dfRY * dfRY <= dfR12) { // If the test point is close to the grid node, use the point // value directly as a node value to avoid singularity. if (CPLIsEqual(dfR2, 0.0)) { (*pdfValue) = padfZ[i]; return CE_None; } else { const double dfW = pow(sqrt(dfR2), dfPower); dfNominator += padfZ[i] / dfW; dfDenominator += 1.0 / dfW; n++; if (nMaxPoints > 0 && n > nMaxPoints) break; } } } if (n < ((GDALGridInverseDistanceToAPowerOptions*)poOptions)->nMinPoints || dfDenominator == 0.0) { (*pdfValue) = ((GDALGridInverseDistanceToAPowerOptions*)poOptions)->dfNoDataValue; } else (*pdfValue) = dfNominator / dfDenominator; return CE_None; }
OGRErr OGRSpatialReference::exportToPanorama( long *piProjSys, long *piDatum, long *piEllips, long *piZone, double *padfPrjParams ) const { CPLAssert( padfPrjParams ); const char *pszProjection = GetAttrValue("PROJECTION"); /* -------------------------------------------------------------------- */ /* Fill all projection parameters with zero. */ /* -------------------------------------------------------------------- */ int i; *piDatum = 0L; *piEllips = 0L; *piZone = 0L; for ( i = 0; i < 7; i++ ) padfPrjParams[i] = 0.0; /* ==================================================================== */ /* Handle the projection definition. */ /* ==================================================================== */ if( IsLocal() ) *piProjSys = PAN_PROJ_NONE; else if( pszProjection == NULL ) { #ifdef DEBUG CPLDebug( "OSR_Panorama", "Empty projection definition, considered as Geographic" ); #endif *piProjSys = PAN_PROJ_NONE; } else if( EQUAL(pszProjection, SRS_PT_MERCATOR_1SP) ) { *piProjSys = PAN_PROJ_MERCAT; padfPrjParams[3] = TO_RADIANS * GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ); padfPrjParams[0] = TO_RADIANS * GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ); padfPrjParams[4] = GetNormProjParm( SRS_PP_SCALE_FACTOR, 1.0 ); padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_POLAR_STEREOGRAPHIC) ) { *piProjSys = PAN_PROJ_PS; padfPrjParams[3] = TO_RADIANS * GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ); padfPrjParams[2] = TO_RADIANS * GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ); padfPrjParams[4] = GetNormProjParm( SRS_PP_SCALE_FACTOR, 1.0 ); padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_POLYCONIC) ) { *piProjSys = PAN_PROJ_POLYC; padfPrjParams[3] = TO_RADIANS * GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ); padfPrjParams[2] = TO_RADIANS * GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ); padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_EQUIDISTANT_CONIC) ) { *piProjSys = PAN_PROJ_EC; padfPrjParams[0] = TO_RADIANS * GetNormProjParm( SRS_PP_STANDARD_PARALLEL_1, 0.0 ); padfPrjParams[1] = TO_RADIANS * GetNormProjParm( SRS_PP_STANDARD_PARALLEL_2, 0.0 ); padfPrjParams[3] = TO_RADIANS * GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ); padfPrjParams[2] = TO_RADIANS * GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ); padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_LAMBERT_CONFORMAL_CONIC_2SP) ) { *piProjSys = PAN_PROJ_LCC; padfPrjParams[0] = TO_RADIANS * GetNormProjParm( SRS_PP_STANDARD_PARALLEL_1, 0.0 ); padfPrjParams[1] = TO_RADIANS * GetNormProjParm( SRS_PP_STANDARD_PARALLEL_2, 0.0 ); padfPrjParams[3] = TO_RADIANS * GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ); padfPrjParams[2] = TO_RADIANS * GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ); padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_TRANSVERSE_MERCATOR) ) { int bNorth; *piZone = GetUTMZone( &bNorth ); if( *piZone != 0 ) { *piProjSys = PAN_PROJ_UTM; if( !bNorth ) *piZone = - *piZone; } else { *piProjSys = PAN_PROJ_TM; padfPrjParams[3] = TO_RADIANS * GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ); padfPrjParams[2] = TO_RADIANS * GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ); padfPrjParams[4] = GetNormProjParm( SRS_PP_SCALE_FACTOR, 1.0 ); padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } } else if( EQUAL(pszProjection, SRS_PT_WAGNER_I) ) { *piProjSys = PAN_PROJ_WAG1; padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_STEREOGRAPHIC) ) { *piProjSys = PAN_PROJ_STEREO; padfPrjParams[3] = TO_RADIANS * GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ); padfPrjParams[2] = TO_RADIANS * GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ); padfPrjParams[4] = GetNormProjParm( SRS_PP_SCALE_FACTOR, 1.0 ); padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_AZIMUTHAL_EQUIDISTANT) ) { *piProjSys = PAN_PROJ_AE; padfPrjParams[3] = TO_RADIANS * GetNormProjParm( SRS_PP_LONGITUDE_OF_CENTER, 0.0 ); padfPrjParams[0] = TO_RADIANS * GetNormProjParm( SRS_PP_LATITUDE_OF_CENTER, 0.0 ); padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_GNOMONIC) ) { *piProjSys = PAN_PROJ_GNOMON; padfPrjParams[3] = TO_RADIANS * GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ); padfPrjParams[2] = TO_RADIANS * GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ); padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_MOLLWEIDE) ) { *piProjSys = PAN_PROJ_MOLL; padfPrjParams[3] = TO_RADIANS * GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ); padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_LAMBERT_AZIMUTHAL_EQUAL_AREA) ) { *piProjSys = PAN_PROJ_LAEA; padfPrjParams[3] = TO_RADIANS * GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ); padfPrjParams[0] = TO_RADIANS * GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ); padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_EQUIRECTANGULAR) ) { *piProjSys = PAN_PROJ_EQC; padfPrjParams[3] = TO_RADIANS * GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ); padfPrjParams[0] = TO_RADIANS * GetNormProjParm( SRS_PP_LATITUDE_OF_ORIGIN, 0.0 ); padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_CYLINDRICAL_EQUAL_AREA) ) { *piProjSys = PAN_PROJ_CEA; padfPrjParams[3] = TO_RADIANS * GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ); padfPrjParams[2] = TO_RADIANS * GetNormProjParm( SRS_PP_STANDARD_PARALLEL_1, 0.0 ); padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } else if( EQUAL(pszProjection, SRS_PT_IMW_POLYCONIC) ) { *piProjSys = PAN_PROJ_IMWP; padfPrjParams[3] = TO_RADIANS * GetNormProjParm( SRS_PP_CENTRAL_MERIDIAN, 0.0 ); padfPrjParams[0] = TO_RADIANS * GetNormProjParm( SRS_PP_LATITUDE_OF_1ST_POINT, 0.0 ); padfPrjParams[1] = TO_RADIANS * GetNormProjParm( SRS_PP_LATITUDE_OF_2ND_POINT, 0.0 ); padfPrjParams[5] = GetNormProjParm( SRS_PP_FALSE_EASTING, 0.0 ); padfPrjParams[6] = GetNormProjParm( SRS_PP_FALSE_NORTHING, 0.0 ); } // Projection unsupported by "Panorama" GIS else { CPLDebug( "OSR_Panorama", "Projection \"%s\" unsupported by \"Panorama\" GIS. " "Geographic system will be used.", pszProjection ); *piProjSys = PAN_PROJ_NONE; } /* -------------------------------------------------------------------- */ /* Translate the datum. */ /* -------------------------------------------------------------------- */ const char *pszDatum = GetAttrValue( "DATUM" ); if ( pszDatum == NULL ) { *piDatum = PAN_DATUM_NONE; *piEllips = PAN_ELLIPSOID_NONE; } else if ( EQUAL( pszDatum, "Pulkovo_1942" ) ) { *piDatum = PAN_DATUM_PULKOVO42; *piEllips = PAN_ELLIPSOID_KRASSOVSKY; } else if( EQUAL( pszDatum, SRS_DN_WGS84 ) ) { *piDatum = PAN_DATUM_WGS84; *piEllips = PAN_ELLIPSOID_WGS84; } // If not found well known datum, translate ellipsoid else { double dfSemiMajor = GetSemiMajor(); double dfInvFlattening = GetInvFlattening(); size_t i; #ifdef DEBUG CPLDebug( "OSR_Panorama", "Datum \"%s\" unsupported by \"Panorama\" GIS. " "Try to translate ellipsoid definition.", pszDatum ); #endif for ( i = 0; i < NUMBER_OF_ELLIPSOIDS; i++ ) { if ( aoEllips[i] ) { double dfSM = 0.0; double dfIF = 1.0; if ( OSRGetEllipsoidInfo( aoEllips[i], NULL, &dfSM, &dfIF ) == OGRERR_NONE && CPLIsEqual(dfSemiMajor, dfSM) && CPLIsEqual(dfInvFlattening, dfIF) ) { *piEllips = i; break; } } } if ( i == NUMBER_OF_ELLIPSOIDS ) // Didn't found matches. { #ifdef DEBUG CPLDebug( "OSR_Panorama", "Ellipsoid \"%s\" unsupported by \"Panorama\" GIS.", pszDatum ); #endif *piDatum = PAN_DATUM_NONE; *piEllips = PAN_ELLIPSOID_NONE; } } return OGRERR_NONE; }
int main(int argc, char *argv[]) { const char *pszInputfile = NULL; const char *pszOutputfile = NULL; const char *pszFormat = "GTiff"; int bProgress = FALSE; int bMaskNoData = FALSE; GDALDatasetH hSrcDS; GDALDatasetH hMaskDS; GDALDriverH hDriver; double adfGeoTransform[6]; const char *pszWkt; int nXSize, nYSize; int nMaskCount = 0; int nPixelsMasked = 0; double adfMaskValues[1024]; int anMaskFound[1024]; int bMaskProvided = FALSE; double dfNoData; int i, j, k; GDALRasterBandH hSrcBand, hMaskBand; double *padfScanline; unsigned char *pabyScanline; int nHasNoData; char** papszCreationOptions = NULL; i = 1; while( i < argc ) { if( EQUAL( argv[i], "-p" ) ) { bProgress = TRUE; } else if( EQUAL( argv[i], "-mask_nodata" ) ) { bMaskNoData = TRUE; bMaskProvided = TRUE; } else if( EQUAL( argv[i], "-of" ) ) { pszFormat = argv[++i]; } else if( EQUAL( argv[i], "-co" ) ) { papszCreationOptions = CSLAddString(papszCreationOptions, argv[++i]); } else if( EQUAL( argv[i], "-h" ) ) { Usage(); } else if( pszInputfile == NULL ) { pszInputfile = argv[i]; } else if( pszOutputfile == NULL ) { pszOutputfile = argv[i]; } else { adfMaskValues[nMaskCount++] = atof(argv[i]); bMaskProvided = TRUE; } //else //{ // Usage(); //} i++; } if( pszInputfile == NULL ) { fprintf( stderr, "No input file provided\n"); Usage(); } if( pszOutputfile == NULL ) { fprintf( stderr, "No output file provided\n" ); Usage(); } GDALAllRegister(); hSrcDS = GDALOpen(pszInputfile, GA_ReadOnly); if(hSrcDS == NULL) { CPLError(CE_Fatal, CPLE_OpenFailed, "Failed to open source dataset"); } hDriver = GDALGetDriverByName(pszFormat); if(hDriver == NULL) { CPLError(CE_Fatal, CPLE_NotSupported, "Failed to load output driver"); } nXSize = GDALGetRasterXSize(hSrcDS); nYSize = GDALGetRasterYSize(hSrcDS); hMaskDS = GDALCreate(hDriver, pszOutputfile, nXSize, nYSize, 1, GDT_Byte, papszCreationOptions); pszWkt = GDALGetProjectionRef(hSrcDS); if(pszWkt != NULL) { GDALSetProjection(hMaskDS, pszWkt); } if(GDALGetGeoTransform(hSrcDS, adfGeoTransform) == CE_None) { GDALSetGeoTransform(hMaskDS, adfGeoTransform); } hSrcBand = GDALGetRasterBand(hSrcDS, 1); hMaskBand = GDALGetRasterBand(hMaskDS, 1); nHasNoData = FALSE; if(bMaskNoData) { dfNoData = GDALGetRasterNoDataValue(hSrcBand, &nHasNoData); if(nHasNoData) { adfMaskValues[nMaskCount++] = dfNoData; } } for(i = 0;i < nMaskCount;i++) { anMaskFound[i] = 0; } padfScanline = (double*)CPLMalloc(nXSize * sizeof(double)); pabyScanline = (unsigned char*)CPLMalloc(nXSize * sizeof(unsigned char)); if(bProgress) { GDALTermProgress(0.0, NULL, NULL); } for(i = 0;i < nYSize;i++) { GDALRasterIO(hSrcBand, GF_Read, 0, i, nXSize, 1, padfScanline, nXSize, 1, GDT_Float64, 0, 0); for(j = 0; j < nXSize;j++) { pabyScanline[j] = 255; for(k = 0; k < nMaskCount; k++) { if(CPLIsEqual(padfScanline[j], adfMaskValues[k])) { pabyScanline[j] = 0; anMaskFound[k]++; nPixelsMasked++; break; } } } GDALRasterIO(hMaskBand, GF_Write, 0, i, nXSize, 1, pabyScanline, nXSize, 1, GDT_Byte, 0, 0); if(bProgress) { GDALTermProgress((double)i / (double)nYSize, NULL, NULL); } } if(bProgress) { GDALTermProgress(1.0, NULL, NULL); } for(i = 0; i < nMaskCount;i++) { CPLDebug("addmask", "%d pixels were masked for value: %lf", anMaskFound[i], adfMaskValues[i]); } CPLDebug("addmask", "%d pixels were masked total", nPixelsMasked); CPLFree(padfScanline); CPLFree(pabyScanline); GDALClose(hSrcDS); GDALClose(hMaskDS); }