bool vtTin::ConvertProjection(const vtProjection &proj_new) { // Create conversion object ScopedOCTransform trans(CreateCoordTransform(&m_proj, &proj_new)); if (!trans) return false; // inconvertible projections int size = NumVerts(); for (int i = 0; i < size; i++) { DPoint2 &p = m_vert[i]; trans->Transform(1, &p.x, &p.y); } // adopt new projection m_proj = proj_new; return true; }
bool vtUtilityMap::TransformTo(vtProjection &proj) { // Convert from (usually, Wgs84 Geographic) to what we need. ScopedOCTransform trans(CreateCoordTransform(&m_proj, &proj)); if (!trans) { VTLOG1(" Couldn't transform coordinates\n"); return false; } for (uint i = 0; i < m_Poles.size(); i++) { vtPole *pole = m_Poles[i]; trans->Transform(1, &pole->m_p.x, &pole->m_p.y); } // Adopt new projection m_proj = proj; return true; }
// A useful method to set the extents (in local CRS) and the corners // (in Geo WGS84) at the same time. bool MiniDatabuf::SetBounds(const vtProjection &proj, const DRECT &extents) { // First, set the extent rectangle set_extents((float)extents.left, (float)extents.right, (float)extents.bottom, (float)extents.top); // Create transform from local to Geo-WGS84 vtProjection geo; geo.SetWellKnownGeogCS("WGS84"); OCT *trans = CreateCoordTransform(&proj, &geo); if (!trans) return false; // Convert each corner as a point DPoint2 sw_corner, se_corner, nw_corner, ne_corner; sw_corner.Set(extents.left, extents.bottom); trans->Transform(1, &sw_corner.x, &sw_corner.y); se_corner.Set(extents.right, extents.bottom); trans->Transform(1, &se_corner.x, &se_corner.y); nw_corner.Set(extents.left, extents.top); trans->Transform(1, &nw_corner.x, &nw_corner.y); ne_corner.Set(extents.right, extents.top); trans->Transform(1, &ne_corner.x, &ne_corner.y); set_LLWGS84corners((float)sw_corner.x, (float)sw_corner.y, (float)se_corner.x, (float)se_corner.y, (float)nw_corner.x, (float)nw_corner.y, (float)ne_corner.x, (float)ne_corner.y); delete trans; return true; }
bool vtUtilityMap::WriteOSM(const char *pathname) { FILE *fp = fopen(pathname, "wb"); if (!fp) return false; // OSM only understands Geographic WGS84, so convert to that. vtProjection wgs84_geo; wgs84_geo.SetGeogCSFromDatum(EPSG_DATUM_WGS84); ScopedOCTransform trans(CreateCoordTransform(&m_proj, &wgs84_geo)); if (!trans) { VTLOG1(" Couldn't transform coordinates\n"); return false; } fprintf(fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); fprintf(fp, "<osm version=\"0.6\" generator=\"VTP %s\">\n", VTP_VERSION); for (uint i = 0; i < m_Poles.size(); i++) { const vtPole *pole = m_Poles[i]; DPoint2 p = pole->m_p; trans->Transform(1, &p.x, &p.y); fprintf(fp, " <node id=\"%d\" lat=\"%.8lf\" lon=\"%.8lf\" version=\"1\">\n", pole->m_id, p.y, p.x); fprintf(fp, " <tag k=\"power\" v=\"tower\"/>\n"); const int num_tags = pole->NumTags(); for (uint j = 0; j < pole->NumTags(); j++) { fprintf(fp, " tag k=\"%s\" v=\"%s\"/>\n", (const char *) pole->GetTag(j)->name, (const char *) pole->GetTag(j)->value); } fprintf(fp, " </node>\n"); } for (uint i = 0; i < m_Lines.size(); i++) { const vtLine *line = m_Lines[i]; const uint num_poles = line->m_poles.size(); const uint num_tags = line->NumTags(); fprintf(fp, " <way id=\"%d\" version=\"1\">\n", line->m_id); for (uint j = 0; j < num_poles; j++) fprintf(fp, " <nd ref=\"%d\"/>\n", line->m_poles[j]->m_id); fprintf(fp, " <tag k=\"power\" v=\"line\"/>\n"); for (uint j = 0; j < line->NumTags(); j++) { fprintf(fp, " tag k=\"%s\" v=\"%s\"/>\n", (const char *) line->GetTag(j)->name, (const char *) line->GetTag(j)->value); } fprintf(fp, " </way>\n"); } fprintf(fp, "</osm>\n"); fclose(fp); return true; }
bool WriteTilesetHeader(const char *filename, int cols, int rows, int lod0size, const DRECT &area, const vtProjection &proj, float minheight, float maxheight, LODMap *lodmap, bool bJPEG) { FILE *fp = vtFileOpen(filename, "wb"); if (!fp) return false; fprintf(fp, "[TilesetDescription]\n"); fprintf(fp, "Columns=%d\n", cols); fprintf(fp, "Rows=%d\n", rows); fprintf(fp, "LOD0_Size=%d\n", lod0size); fprintf(fp, "Extent_Left=%.16lg\n", area.left); fprintf(fp, "Extent_Right=%.16lg\n", area.right); fprintf(fp, "Extent_Bottom=%.16lg\n", area.bottom); fprintf(fp, "Extent_Top=%.16lg\n", area.top); // write CRS, but pretty it up a bit OGRSpatialReference *poSimpleClone = proj.Clone(); poSimpleClone->GetRoot()->StripNodes( "AXIS" ); poSimpleClone->GetRoot()->StripNodes( "AUTHORITY" ); char *wkt; poSimpleClone->exportToWkt(&wkt); fprintf(fp, "CRS=%s\n", wkt); OGRFree(wkt); // Free CRS delete poSimpleClone; // For elevation tilesets, also write vertical extents if (minheight != INVALID_ELEVATION) { fprintf(fp, "Elevation_Min=%.f\n", minheight); fprintf(fp, "Elevation_Max=%.f\n", maxheight); } if (lodmap != NULL) { int mmin, mmax; for (int i = 0; i < rows; i++) { fprintf(fp, "RowLODs %2d:", i); for (int j = 0; j < cols; j++) { lodmap->get(j, i, mmin, mmax); fprintf(fp, " %d/%d", mmin, mmax); } fprintf(fp, "\n"); } } // create a transformation that will map from the current projection to Lat/Lon WGS84 vtProjection proj_llwgs84; proj_llwgs84.SetWellKnownGeogCS("WGS84"); OCT *LLWGS84transform=CreateCoordTransform(&proj,&proj_llwgs84); // write center point of the tileset in Lat/Lon WGS84 // this is helpful for libMini to compute an approximate translation double cx=(area.left+area.right)/2; double cy=(area.bottom+area.top)/2; if (LLWGS84transform->Transform(1,&cx,&cy)==1) fprintf(fp, "CenterPoint_LLWGS84=(%.16lg,%.16lg)\n",cx,cy); // write north point of the tileset in Lat/Lon WGS84 // this is helpful for libMini to compute an approximate rotation double nx=(area.left+area.right)/2; double ny=area.top; if (LLWGS84transform->Transform(1,&nx,&ny)==1) fprintf(fp, "NorthPoint_LLWGS84=(%.16lg,%.16lg)\n",nx,ny); // delete Lat/Lon WGS84 transformation delete LLWGS84transform; // write CRS info // this is helpful for libMini to easily identify the coordinate reference system // supported CRS are: Geographic, UTM, Mercator const int crs=mapCRS2MINI(proj); const int datum=mapEPSG2MINI(proj.GetDatum()); const int utmzone=proj.GetUTMZone(); fprintf(fp, "CoordSys=(%d,%d,%d)\n",crs,datum,utmzone); if (bJPEG) fprintf(fp, "Format=JPEG\n"); else fprintf(fp, "Format=DB\n"); fclose(fp); return true; }