static bool TransformTiffCorner(GTIF * gtif, GTIFDefn * defn, double x, double y, double& outLon, double& outLat) { /* Try to transform the coordinate into PCS space */ if( !GTIFImageToPCS( gtif, &x, &y ) ) return false; if( defn->Model == ModelTypeGeographic ) { outLon = x; outLat = y; return true; } else { if( GTIFProj4ToLatLong( defn, 1, &x, &y ) ) { outLon = x; outLat = y; return true; } int size = 0; tagtype_t type = TYPE_UNKNOWN; int key_count = GTIFKeyInfo(gtif, GTCitationGeoKey, &size, &type); if(key_count > 0 && key_count < 1024 && type == TYPE_ASCII && size == 1) { vector<char> ascii(key_count); int r = GTIFKeyGet(gtif, GTCitationGeoKey, &ascii[0], 0, key_count); if(r == key_count) { DebugAssert(ascii.back() == 0); string citation = string(&ascii[0]); if(citation == "PCS Name = WGS_1984_Web_Mercator_Auxiliary_Sphere") { char ** args = CSLTokenizeStringComplex("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs", " +", TRUE, FALSE); PJ * psPJ = pj_init( CSLCount(args), args ); CSLDestroy(args); if(psPJ) { projUV sUV; sUV.u = x; sUV.v = y; sUV = pj_inv( sUV, psPJ ); outLon = sUV.u * RAD_TO_DEG; outLat = sUV.v * RAD_TO_DEG; pj_free(psPJ); return true; } } } } } return false; }
static int GTIFReportACorner( GTIF *gtif, GTIFDefn *defn, FILE * fp_out, const char * corner_name, double x, double y, int inv_flag, int dec_flag ) { double x_saved, y_saved; /* Try to transform the coordinate into PCS space */ if( !GTIFImageToPCS( gtif, &x, &y ) ) return FALSE; x_saved = x; y_saved = y; fprintf( fp_out, "%-13s ", corner_name ); if( defn->Model == ModelTypeGeographic ) { if (dec_flag) { fprintf( fp_out, "(%s,", GTIFDecToDDec( x, "Long", 7 ) ); fprintf( fp_out, "%s)\n", GTIFDecToDDec( y, "Lat", 7 ) ); } else { fprintf( fp_out, "(%s,", GTIFDecToDMS( x, "Long", 2 ) ); fprintf( fp_out, "%s)\n", GTIFDecToDMS( y, "Lat", 2 ) ); } } else { fprintf( fp_out, "(%12.3f,%12.3f)", x, y ); if( GTIFProj4ToLatLong( defn, 1, &x, &y ) ) { if (dec_flag) { fprintf( fp_out, " (%s,", GTIFDecToDDec( x, "Long", 7 ) ); fprintf( fp_out, "%s)", GTIFDecToDDec( y, "Lat", 7 ) ); } else { fprintf( fp_out, " (%s,", GTIFDecToDMS( x, "Long", 2 ) ); fprintf( fp_out, "%s)", GTIFDecToDMS( y, "Lat", 2 ) ); } } fprintf( fp_out, "\n" ); } if( inv_flag && GTIFPCSToImage( gtif, &x_saved, &y_saved ) ) { fprintf( fp_out, " inverse (%11.3f,%11.3f)\n", x_saved, y_saved ); } return TRUE; }
static GeoPoint TiffPixelToGeoPoint(GTIF >if, GTIFDefn &defn, double x, double y) { if (!GTIFImageToPCS(>if, &x, &y)) return GeoPoint::Invalid(); if (defn.Model != ModelTypeGeographic && !GTIFProj4ToLatLong(&defn, 1, &x, &y)) return GeoPoint::Invalid(); return GeoPoint(Angle::Degrees(x), Angle::Degrees(y)); }
static void WriteTFWFile( GTIF * gtif, const char * tif_filename ) { char tfw_filename[1024]; int i; double adfCoeff[6], x, y; FILE *fp; /* * form .tfw filename */ strncpy( tfw_filename, tif_filename, sizeof(tfw_filename)-4 ); for( i = strlen(tfw_filename)-1; i > 0; i-- ) { if( tfw_filename[i] == '.' ) { strcpy( tfw_filename + i, ".tfw" ); break; } } if( i <= 0 ) strcat( tfw_filename, ".tfw" ); /* * Compute the coefficients. */ x = 0.5; y = 0.5; if( !GTIFImageToPCS( gtif, &x, &y ) ) return; adfCoeff[4] = x; adfCoeff[5] = y; x = 1.5; y = 0.5; if( !GTIFImageToPCS( gtif, &x, &y ) ) return; adfCoeff[0] = x - adfCoeff[4]; adfCoeff[1] = y - adfCoeff[5]; x = 0.5; y = 1.5; if( !GTIFImageToPCS( gtif, &x, &y ) ) return; adfCoeff[2] = x - adfCoeff[4]; adfCoeff[3] = y - adfCoeff[5]; /* * Write out the coefficients. */ fp = fopen( tfw_filename, "wt" ); if( fp == NULL ) { perror( "fopen" ); fprintf( stderr, "Failed to open TFW file `%s'\n", tfw_filename ); return; } for( i = 0; i < 6; i++ ) fprintf( fp, "%24.10f\n", adfCoeff[i] ); fclose( fp ); }
int main(int argc, char *argv[]) { char *fname = NULL; char *outfile = NULL; TIFF *tif=(TIFF*)0; /* TIFF-level descriptor */ GTIF *gtif=(GTIF*)0; /* GeoKey-level descriptor */ int i, j, norm_print_flag = 0, proj4_print_flag = 0; int tfw_flag = 0, inv_flag = 0, dec_flag = 1; int st_test_flag = 0; size_t npixels; int16* elevations; int x, y, skip; GTIFDefn defn; FILE* out; /* * Handle command line options. */ for( i = 1; i < argc; i++ ) { if( fname == NULL && argv[i][0] != '-' ) fname = argv[i]; else { Usage(); } } if( fname == NULL) Usage(); skip = 5; /* * Open the file, read the GeoTIFF information, and print to stdout. */ int flength = strlen(fname); outfile = (char *) malloc(flength); strncpy(outfile,fname,flength-4); strcat(outfile, ".asc\0"); out = fopen(outfile, "w"); printf("Writing to %s\n\n",outfile); free(outfile); tif=XTIFFOpen(fname,"r"); if (!tif) goto failure; gtif = GTIFNew(tif); if (!gtif) { fprintf(stderr,"failed in GTIFNew\n"); goto failure; } /* dump the GeoTIFF metadata to std out */ GTIFPrint(gtif,0,0); if( GTIFGetDefn( gtif, &defn ) ) { uint32 xsize, ysize; printf( "\n" ); GTIFPrintDefn( &defn, stdout ); if( proj4_print_flag ) { printf( "\n" ); printf( "PROJ.4 Definition: %s\n", GTIFGetProj4Defn(&defn)); } int count, orient; double* data; TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &xsize ); TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &ysize ); TIFFGetField( tif, TIFFTAG_GEOPIXELSCALE, &count, &data); TIFFGetField( tif, TIFFTAG_ORIENTATION, &orient ); printf("Orientation:%d\n",orient); GTIFPrintCorners( gtif, &defn, stdout, xsize, ysize, inv_flag, dec_flag ); const char* project = "ASTERGDEM"; const char* yymmdd = "090629"; double originx, originy; GTIFImageToPCS( gtif, &originx, &originy); int lat = (int)originy*1000; int lon = (int)originx*1000; int xmin = 0; int ymin = 0; int nx = xsize/skip + 1; int ny = ysize/skip + 1; int dx = 30 * skip; int dy = 30 * skip; int16 elev = 0; fprintf(out, "%12s%12s%7d%7d%7d%7d%7d%7d%7d%7d\n", project, yymmdd, lat, lon, xmin, ymin, nx, ny, dx,dy); npixels = xsize * ysize; int16* buf; tsample_t sample; int nbytes; nbytes = TIFFScanlineSize(tif); buf = (int16*) _TIFFmalloc(nbytes); elevations = (int16*) _TIFFmalloc(npixels * sizeof (int16)); if (elevations != NULL) { for( y = 0; y < ysize; y++ ) { uint32 row = ysize - 1 - y; TIFFReadScanline(tif, buf, row, sample); for( x = 0; x < xsize; x++ ) { elevations[y*xsize + x] = buf[x]; } } _TIFFfree(buf); } for( y = 0; y < ysize; y+=skip ) { for( x = 0; x < xsize; x+=skip ) { fprintf(out, "%6d", elevations[y*xsize + x]); } fprintf(out, "\n"); } _TIFFfree(elevations); } Success: GTIFFree(gtif); if( st_test_flag ) ST_Destroy( (ST_TIFF *) tif ); else XTIFFClose(tif); GTIFDeaccessCSV(); return 0; failure: fprintf(stderr,"failure in listgeo\n"); if (tif) XTIFFClose(tif); if (gtif) GTIFFree(gtif); GTIFDeaccessCSV(); return 1; }
bool DEM::readDem(char* fname) { demFilename = fname; TIFF *tif=(TIFF*)0; /* TIFF-level descriptor */ GTIF *gtif=(GTIF*)0; /* GeoKey-level descriptor */ int proj4_print_flag = 0; int inv_flag = 0, dec_flag = 1; int st_test_flag = 0; GTIFDefn defn; /* * Open the file, read the GeoTIFF information, and print some info to stdout. */ tif=XTIFFOpen(fname,"r"); if (!tif) goto failure; gtif = GTIFNew(tif); if (!gtif) { fprintf(stderr,"failed in GTIFNew\n"); goto failure; } /* dump the GeoTIFF metadata to std out */ GTIFPrint(gtif,0,0); if( GTIFGetDefn( gtif, &defn ) ) { printf( "\n" ); GTIFPrintDefn( &defn, stdout ); if( proj4_print_flag ) { printf( "\n" ); printf( "PROJ.4 Definition: %s\n", GTIFGetProj4Defn(&defn)); } int count, orient; double* data; TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &xsize ); TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &ysize ); TIFFGetField( tif, TIFFTAG_GEOPIXELSCALE, &count, &data); TIFFGetField( tif, TIFFTAG_ORIENTATION, &orient ); printf("Orientation:%d\n",orient); GTIFPrintCorners( gtif, &defn, stdout, xsize, ysize, inv_flag, dec_flag ); double originx = 0.0; double originy = ysize; GTIFImageToPCS( gtif, &originx, &originy); refLat = originy; refLon = originx; npixels = xsize * ysize; int16* buf; tsample_t sample; int nbytes; nbytes = TIFFScanlineSize(tif); buf = (int16*) _TIFFmalloc(nbytes); elevations = (int16*) _TIFFmalloc(npixels * sizeof (int16)); if (elevations != NULL) { for(unsigned int y = 0; y < ysize; y++ ) { uint32 row = ysize - 1 - y; TIFFReadScanline(tif, buf, row, sample); for(unsigned int x = 0; x < xsize; x++ ) { elevations[y*xsize + x] = buf[x]; } } _TIFFfree(buf); } _TIFFfree(elevations); } GTIFFree(gtif); if( st_test_flag ) ST_Destroy( (ST_TIFF *) tif ); else XTIFFClose(tif); GTIFDeaccessCSV(); return true; failure: fprintf(stderr,"failure in listgeo\n"); if (tif) XTIFFClose(tif); if (gtif) GTIFFree(gtif); GTIFDeaccessCSV(); return false; }
void CGeoImageView::EngineSetup(void) { Ogre::Root *Root = ((CGeoImageApp*)AfxGetApp())->m_Engine->GetRoot(); Ogre::SceneManager *SceneManager = NULL; SceneManager = Root->createSceneManager(Ogre::ST_GENERIC, "MFCOgre"); // // Create a render window // This window should be the current ChildView window using the externalWindowHandle // value pair option. // Ogre::NameValuePairList parms; parms["externalWindowHandle"] = Ogre::StringConverter::toString((long)m_hWnd); parms["vsync"] = "true"; CRect rect; GetClientRect(&rect); Ogre::RenderTarget *RenderWindow = Root->getRenderTarget("Ogre in MFC"); if (RenderWindow == NULL) { try { m_RenderWindow = Root->createRenderWindow("Ogre in MFC", rect.Width(), rect.Height(), false, &parms); } catch(...) { MessageBox("Cannot initialize\nCheck that graphic-card driver is up-to-date", "Initialize Render System", MB_OK | MB_ICONSTOP); exit(EXIT_SUCCESS); } } // Load resources Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); // Create the camera m_Camera = SceneManager->createCamera("Camera"); m_Camera->setNearClipDistance(0.5); m_Camera->setFarClipDistance(5000); m_Camera->setCastShadows(false); m_Camera->setUseRenderingDistance(true); // m_Camera->setPosition(Ogre::Vector3(320.0, 240.0, 500.0)); Ogre::SceneNode *CameraNode = NULL; CameraNode = SceneManager->getRootSceneNode()->createChildSceneNode("CameraNode"); Ogre::Viewport* Viewport = NULL; if (0 == m_RenderWindow->getNumViewports()) { Viewport = m_RenderWindow->addViewport(m_Camera); Viewport->setBackgroundColour(Ogre::ColourValue(0.8f, 1.0f, 0.8f)); } // Alter the camera aspect ratio to match the viewport m_Camera->setAspectRatio(Ogre::Real(rect.Width()) / Ogre::Real(rect.Height())); TIFF *Tif = (TIFF*)0; /* TIFF-level descriptor */ GTIF *GTif = (GTIF*)0; /* GeoKey-level descriptor */ CString SourcePath = "C:\\Users\\Ilya\\Documents\\Visual Studio 2010\\Projects\\Recipes\\media\\materials\\textures\\o41078a1.tif"; int ImageWidth; int ImageHeight; double LowerLeftX; double LowerLeftY; double UpperRightX; double UpperRightY; int Xpos; int Ypos; Tif = XTIFFOpen((LPCSTR)SourcePath, "r"); GTif = GTIFNew(Tif); GTIFDefn Definition; GTIFGetDefn(GTif, &Definition); GTIFPrint(GTif, 0, 0); TIFFGetField( Tif, TIFFTAG_IMAGEWIDTH, &ImageWidth); TIFFGetField( Tif, TIFFTAG_IMAGELENGTH, &ImageHeight); int res = 0; double LowerRightX = ImageWidth; double LowerRightY = ImageHeight; res = GTIFImageToPCS(GTif, &LowerRightX, &LowerRightY); //Lower Left LowerLeftX = 0.0; LowerLeftY = ImageHeight; res = GTIFImageToPCS(GTif, &LowerLeftX, &LowerLeftY); //Upper Right UpperRightX = ImageWidth; UpperRightY = 0.0; res = GTIFImageToPCS(GTif, &UpperRightX, &UpperRightY); double UpperLeftX = 0.0; double UpperLeftY = 0.0; res = GTIFImageToPCS(GTif, &UpperLeftX, &UpperLeftY); Ogre::ManualObject *Terrain = SceneManager->createManualObject("Terrain"); Terrain->setDynamic(false); Terrain->begin("Terrain", Ogre::RenderOperation::OT_TRIANGLE_FAN); #define MIN(x,y) (((x) < (y)) ? (x) : (y)) #define MAX(x,y) (((x) > (y)) ? (x) : (y)) #define SW 0 #define NW 1 #define NE 2 #define SE 3 FILE *File = NULL; char Dummy[160]; char inText[24]; char *Dest; int base[2048]; /* array of base elevations */ char mapLabel[145]; int DEMlevel, elevationPattern, groundSystem, groundZone; double projectParams[15]; int planeUnitOfMeasure, elevUnitOfMeasure, polygonSizes; double groundCoords[4][2], elevBounds[2], localRotation; int accuracyCode; double spatialResolution[3]; int profileDimension[2]; int firstRow, lastRow; int wcount = 0; double verticalScale = 1.0; /* to stretch or shrink elevations */ double deltaY; char *junk; double eastMost; double westMost; double southMost; double northMost; int eastMostSample; int westMostSample; int southMostSample; int northMostSample; int rowCount, columnCount, r, c; int rowStr = 1; int rowEnd; int colStr = 1; int colEnd; int colInt = 1; int rowInt = 1; int outType = 0; int k,l ; double comp = 0.0; int mod ; int tempInt, lastProfile = 0; float noValue = 0.0f; int profileID[2], profileSize[2]; double planCoords[2], localElevation, elevExtremea[2]; File = fopen("C:\\Users\\Ilya\\Documents\\Visual Studio 2010\\Projects\\Recipes\\media\\geotiff\\karthaus_pa.dem", "r"); fscanf(File, "%144c%6d%6d%6d%6d", mapLabel, /* 1 */ &DEMlevel, /* 2 */ &elevationPattern, /* 3 */ &groundSystem, /* 4 */ &groundZone ); /* 5 */ for (k=0; k<15 ; k++) { fscanf(File,"%24c", inText); /* 6 */ Dest = strchr(inText,'D'); *Dest = 'E'; projectParams[k] = strtod(inText,&junk); } fscanf(File,"%6d%6d%6d", &planeUnitOfMeasure, /* 7 */ &elevUnitOfMeasure, /* 8 */ &polygonSizes); /* 9 */ for (k=0; k < 4 ; k++) for (l=0; l < 2 ; l++) { fscanf(File,"%24c", inText); /* 6 */ Dest = strchr(inText,'D'); *Dest = 'E'; groundCoords[k][l] = strtod(inText,&junk); } fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; elevBounds[0] = strtod(inText,&junk); /* 10 */ fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; elevBounds[1] = strtod(inText,&junk); /* 11 */ fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; localRotation = strtod(inText,&junk); /* 12 */ fscanf(File,"%1d%12le%12le%12le%6d%6d", &accuracyCode, /* 13 */ &spatialResolution[0], /* 14 */ &spatialResolution[1], /* 14 */ &spatialResolution[2], /* 14 */ &profileDimension[0], /* 15 */ &profileDimension[1]); /* 15 */ fscanf(File, "%160c", Dummy); if (spatialResolution[0] == 3.0) { comp = spatialResolution[0] - 1.0 ; deltaY = spatialResolution[1] / 3600. ; } else { comp = 0.; deltaY = spatialResolution[1] ; } eastMost = MAX(groundCoords[NE][0], groundCoords[SE][0]); westMost = MIN(groundCoords[NW][0], groundCoords[SW][0]); northMost = MAX(groundCoords[NE][1], groundCoords[NW][1]); southMost = MIN(groundCoords[SW][1], groundCoords[SE][1]); eastMostSample = ((int) (eastMost / spatialResolution[0])) * (int) spatialResolution[0]; westMostSample = ((int) ((westMost + comp) / spatialResolution[0])) * (int) spatialResolution[0] ; northMostSample = ((int) (northMost / spatialResolution[1])) * (int) spatialResolution[1] ; southMostSample = ((int) ((southMost + comp) / spatialResolution[1])) * (int) spatialResolution[1] ; columnCount = (eastMostSample - westMostSample) / (int) spatialResolution[0] + 1; rowCount = (northMostSample - southMostSample) / (int) spatialResolution[1] + 1; if (columnCount != profileDimension[1]) columnCount =MIN( profileDimension[1], columnCount); for (c = 1; c <= columnCount; c++) { fscanf(File, "%d%d%d%d", &profileID[0], /* 1 */ &profileID[1], /* 1 */ &profileSize[0], /* 2 */ &profileSize[1]); /* 2 */ fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; planCoords[0] = strtod(inText,&junk); /* 3 */ fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; planCoords[1] = strtod(inText,&junk); /* 3 */ fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; localElevation = strtod(inText,&junk); /* 4 */ fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; elevExtremea[0] = strtod(inText,&junk); /* 5 */ fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; elevExtremea[1] = strtod(inText,&junk); /* 5 */ lastProfile = profileID[1]; firstRow = abs(((int) (planCoords[1] - southMostSample)) / (int) spatialResolution[1]); lastRow = firstRow + profileSize[0] - 1; for ( r = 0 ; r < firstRow ; r++ ) base[r] = 0; /* read in all the data for this column */ for (r = firstRow; r <= lastRow; r++ ) { fscanf(File, "%6d", &tempInt); base[r] = tempInt; } double tempFloat; for (r = firstRow; r <= lastRow; r += rowInt) { tempFloat = (float) base[r] * verticalScale; Terrain->position(planCoords[0], tempFloat, planCoords[1]); Ogre::Real u = 0.0; Ogre::Real v = 0.0; if (planCoords[0] > LowerLeftX && planCoords[0] < UpperRightX) { u = (planCoords[0] - LowerLeftX) / (UpperRightX - LowerLeftX); } if (planCoords[1] > LowerLeftY && planCoords[1] < UpperRightY) { v = (planCoords[1] - LowerLeftY) / (UpperRightY - LowerLeftY); } Terrain->textureCoord(u, v); planCoords[1] += deltaY; } } Terrain->end(); fclose(File); Ogre::SceneNode* node = SceneManager->getRootSceneNode()->createChildSceneNode(); node->setPosition(0, 0, 0); node->attachObject(Terrain); Ogre::AxisAlignedBox Box = Terrain->getBoundingBox(); Ogre::Vector3 Center = Box.getCenter(); Ogre::Vector3 Target = Center; Ogre::Vector3 Position = Center; Position[1] += 5000.0; m_Camera->setPosition(Position); m_Camera->lookAt(Target); }