std::pair<UncompressedImage, GeoQuadrilateral> LoadGeoTiff(Path path) { TiffLoader tiff(path); GeoQuadrilateral bounds; { auto gtif = GTIFNew(tiff.Get()); if (gtif == nullptr) throw std::runtime_error("Not a GeoTIFF file"); AtScopeExit(gtif) { GTIFFree(gtif); }; GTIFDefn defn; if (!GTIFGetDefn(gtif, &defn)) throw std::runtime_error("Failed to parse GeoTIFF metadata"); int width, height; tiff.GetField(TIFFTAG_IMAGEWIDTH, width); tiff.GetField(TIFFTAG_IMAGELENGTH, height); bounds.top_left = TiffPixelToGeoPoint(*gtif, defn, 0, 0); bounds.top_right = TiffPixelToGeoPoint(*gtif, defn, width, 0); bounds.bottom_left = TiffPixelToGeoPoint(*gtif, defn, 0, height); bounds.bottom_right = TiffPixelToGeoPoint(*gtif, defn, width, height); if (!bounds.Check()) throw std::runtime_error("Invalid GeoTIFF bounds"); } return std::make_pair(LoadTiff(tiff), bounds); }
int main(int argc, char *argv[]) { char *fname = NULL; TIFF *tif=(TIFF*)0; /* TIFF-level descriptor */ GTIF *gtif=(GTIF*)0; /* GeoKey-level descriptor */ int i, norm_print_flag = 1, proj4_print_flag = 0; int tfw_flag = 0, inv_flag = 0, dec_flag = 0; /* * Handle command line options. */ for( i = 1; i < argc; i++ ) { if( strcmp(argv[i],"-no_norm") == 0 ) norm_print_flag = 0; else if( strcmp(argv[i],"-t") == 0 ) { CSVDirName = argv[++i]; SetCSVFilenameHook( CSVFileOverride ); } else if( strcmp(argv[i],"-tfw") == 0 ) tfw_flag = 1; else if( strcmp(argv[i],"-proj4") == 0 ) proj4_print_flag = 1; else if( strcmp(argv[i],"-i") == 0 ) inv_flag = 1; else if( strcmp(argv[i],"-d") == 0 ) dec_flag = 1; else if( fname == NULL && argv[i][0] != '-' ) fname = argv[i]; else { Usage(); } } if( fname == NULL ) Usage(); /* * Open the file, read the GeoTIFF information, and print to stdout. */ tif=XTIFFOpen(fname,"r"); if (!tif) goto failure; gtif = GTIFNew(tif); if (!gtif) { fprintf(stderr,"failed in GTIFNew\n"); goto failure; } if( tfw_flag ) { WriteTFWFile( gtif, fname ); goto Success; } /* dump the GeoTIFF metadata to std out */ GTIFPrint(gtif,0,0); /* * Capture, and report normalized information if requested. */ if( norm_print_flag ) { GTIFDefn defn; if( GTIFGetDefn( gtif, &defn ) ) { int xsize, ysize; printf( "\n" ); GTIFPrintDefn( &defn, stdout ); if( proj4_print_flag ) { printf( "\n" ); printf( "PROJ.4 Definition: %s\n", GTIFGetProj4Defn(&defn)); } TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &xsize ); TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &ysize ); GTIFPrintCorners( gtif, &defn, stdout, xsize, ysize, inv_flag, dec_flag ); } } Success: GTIFFree(gtif); XTIFFClose(tif); return 0; failure: fprintf(stderr,"failure in listgeo\n"); if (tif) XTIFFClose(tif); if (gtif) GTIFFree(gtif); return 1; }
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 FetchTIFFCornersWithTIFF(TIFF * tiffFile, double corners[8], int& post_pos, int width, int height) { bool retVal = false; GTIF * gtif = GTIFNew(tiffFile); if (gtif) { // GTIFPrint(gtif, pm, NULL); GTIFDefn defn; if( GTIFGetDefn( gtif, &defn ) ) { int xs, ys; double xsize,ysize; TIFFGetField( tiffFile, TIFFTAG_IMAGEWIDTH, &xs ); TIFFGetField( tiffFile, TIFFTAG_IMAGELENGTH, &ys ); uint16 pixel_type; double dx=0.0; double dy=0.0; //If there is the optional width and height if(width > 0 && height > 0) { xsize=width; ysize=height; } else { xsize=xs; ysize=ys; } if (GTIFKeyGet(gtif,GTRasterTypeGeoKey, &pixel_type, 0, 1) != 1) pixel_type=RasterPixelIsArea; // If we are a 'point sampled' file, the upper right edge _IS_ the last pixels! Thus // passing in the number of pixels induces an off-by-one. Cut the size by one to fix this. if(pixel_type == RasterPixelIsPoint) { xsize -= 1.0; ysize -= 1.0; } if(pixel_type==RasterPixelIsArea && post_pos == dem_want_Post) { // This is an area-pixel DEM, but we are going to reinterpret it via pixel centers. // This will INSET the corners of the pixels by 1/2 pixel to the sample centers. dx=0.5; dy=0.5; } if(pixel_type==RasterPixelIsPoint && post_pos == dem_want_Area) { // This is a center post sampled image, but we are going to treat it as area. Each // pixel "sticks out" a bit in its coverage, so extend. dx=-0.5; dy=-0.5; } if(post_pos == dem_want_File) post_pos = (pixel_type==RasterPixelIsPoint) ? dem_want_Post : dem_want_Area; if (TransformTiffCorner(gtif, &defn, dx, ysize-dy, corners[0], corners[1]) && TransformTiffCorner(gtif, &defn, xsize-dx, ysize-dy, corners[2], corners[3]) && TransformTiffCorner(gtif, &defn, dx, dy, corners[4], corners[5]) && TransformTiffCorner(gtif, &defn, xsize-dx, dy, corners[6], corners[7])) { // Ben says: we used to snap round. Since the 'far' tie point is calculated by res * pixels // and res might be 1/1200 or 1/1201, we get floating point crud in our tiff calcs. But // if we aren't known to be on 1-degree boundaries, this snap rounding is just wrong. So: // don't round - we need good precision in other places. Instead, we can round in the raster-import cmd. // corners[0]=round_by_parts_guess(corners[0],xs); // corners[2]=round_by_parts_guess(corners[2],xs); // corners[4]=round_by_parts_guess(corners[4],xs); // corners[6]=round_by_parts_guess(corners[6],xs); // // corners[1]=round_by_parts_guess(corners[1],ys); // corners[3]=round_by_parts_guess(corners[3],ys); // corners[5]=round_by_parts_guess(corners[5],ys); // corners[7]=round_by_parts_guess(corners[7],ys); retVal = true; } } GTIFFree(gtif); } return retVal; }
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); }