/** * Write this image to a JPEG file. Quality setting is the same as libjpeg, * in the range of 0..100. 99 is a typically useful quality setting. */ bool vtImage::WriteJPEG(const char *fname, int quality, bool progress_callback(int)) { #if USE_OSG_FOR_JPG osgDB::Registry *reg = osgDB::Registry::instance(); osgDB::ReaderWriter::Options *opts = reg->getOptions(); if (!opts) { opts = new osgDB::ReaderWriter::Options; opts->ref(); // workaround! otherwise OSG might crash when // closing its DLL, as the options get deleted twice (?) or // perhaps it doesn't like deleting the object WE allocated. } reg->setOptions(opts); vtString str; str.Format("JPEG_QUALITY %d", quality); opts->setOptionString((const char *)str); // fname is a UTF-8 string, but OSG only understands local charset vtString fname_local = UTF8ToLocal(fname); return osgDB::writeImageFile(*this, (const char *) fname_local); #else // TODO: native libjpeg code here return false; #endif }
bool vtUtilityMap::ImportPolesFromSHP(const char *fname) { SHPHandle hSHP; int nEntities, nShapeType; DPoint2 point; // SHPOpen doesn't yet support utf-8 or wide filenames, so convert vtString fname_local = UTF8ToLocal(fname); hSHP = SHPOpen(fname_local, "rb"); if (!hSHP) return false; SHPGetInfo(hSHP, &nEntities, &nShapeType, NULL, NULL); if (nShapeType != SHPT_POINT) return false; for (int i = 0; i < nEntities; i++) { SHPObject *psShape = SHPReadObject(hSHP, i); point.x = psShape->padfX[0]; point.y = psShape->padfY[0]; vtPole *pole = new vtPole; pole->m_p = point; m_Poles.Append(pole); SHPDestroyObject(psShape); } SHPClose(hSHP); return true; }
/** * Unarchives the indicated zipped file. * Each directory and file in the archive is created. * * \param prepend_path A string to be prepended to all output filenames. * * \return -1 on error, otherwise the number of files the archive contained. */ int ExpandZip(const char *archive_fname, const char *prepend_path, bool progress_callback(int)) { // vtUnzip doesn't handle utf8 paths, so convert to local vtString local_fname = UTF8ToLocal(archive_fname); vtUnzip uz; bool success = uz.Open(local_fname); if (!success) return -1; return uz.Extract(true, true, prepend_path, progress_callback); }
bool vtUtilityMap::ImportLinesFromSHP(const char *fname) { SHPHandle hSHP; int nEntities, nShapeType; int i, j, verts; // SHPOpen doesn't yet support utf-8 or wide filenames, so convert vtString fname_local = UTF8ToLocal(fname); hSHP = SHPOpen(fname_local, "rb"); if (!hSHP) return false; SHPGetInfo(hSHP, &nEntities, &nShapeType, NULL, NULL); if (nShapeType != SHPT_ARC) return false; for (i = 0; i < nEntities; i++) { SHPObject *psShape = SHPReadObject(hSHP, i); verts = psShape->nVertices; vtLine *line = new vtLine; line->SetSize(verts); // Store each SHP Poly Coord in Line for (j = 0; j < verts; j++) { line->GetAt(j).x = psShape->padfX[j]; line->GetAt(j).y = psShape->padfY[j]; } SHPDestroyObject(psShape); // Guess source and destination poles by using location line->m_src = ClosestPole(line->GetAt(0)); line->m_dst = ClosestPole(line->GetAt(verts-1)); // avoid degenerate lines if (line->m_src == line->m_dst) { delete line; continue; } // tweak start and end of line to match poles line->SetAt(0, line->m_src->m_p); line->SetAt(verts-1, line->m_dst->m_p); m_Lines.Append(line); } SHPClose(hSHP); return true; }
bool vtImage::WritePNG(const char *fname, bool progress_callback(int)) { #if USE_OSG_FOR_PNG // fname is a UTF-8 string, but OSG only understands local charset vtString fname_local = UTF8ToLocal(fname); return osgDB::writeImageFile(*this, (const char *) fname_local); #else // TODO: native libpng code here return false; #endif }
int GetSHPType(const char *filename) { // SHPOpen doesn't yet support utf-8 or wide filenames, so convert vtString fname_local = UTF8ToLocal(filename); SHPHandle hSHP = SHPOpen(fname_local, "rb"); if (hSHP == NULL) return SHPT_NULL; int nEntities, nShapeType; SHPGetInfo(hSHP, &nEntities, &nShapeType, NULL, NULL); SHPClose(hSHP); return nShapeType; }
/** * Extract point data from a SHP/DBF file and intepret it as a vegetation * layer. This produces a set of vegetation instances. * * The 'opt' parameter contains a description of how the fields in the * imported file are to be interpreted. */ bool vtVegLayer::AddElementsFromSHP_Points(const wxString &filename, const vtProjection &proj, VegPointOptions &opt) { // We will be creating plant instances SetVegType(VLT_Instances); vtSpeciesList *pSpeciesList = g_bld->GetSpeciesList(); vtBioRegion *pBioRegion = g_bld->GetBioRegion(); GetPIA()->SetSpeciesList(pSpeciesList); // SHPOpen doesn't yet support utf-8 or wide filenames, so convert vtString fname_local = UTF8ToLocal(filename.mb_str(wxConvUTF8)); // Open the SHP File SHPHandle hSHP = SHPOpen(fname_local, "rb"); if (hSHP == NULL) return false; // Get number of points and type of data int nElem; int nShapeType; SHPGetInfo(hSHP, &nElem, &nShapeType, NULL, NULL); // Check Shape Type, Veg Layer should be Point data if (nShapeType != SHPT_POINT) return false; // Open DBF File DBFHandle db = DBFOpen(fname_local, "rb"); if (db == NULL) return false; // Confirm that the field types are correct int *pnWidth = 0, *pnDecimals = 0; char pszFieldName[80]; DBFFieldType fieldtype; if (!opt.bFixedSpecies) { // we're going to get species info from a field fieldtype = DBFGetFieldInfo(db, opt.iSpeciesFieldIndex, pszFieldName, pnWidth, pnDecimals); if (opt.iInterpretSpeciesField == 0 || opt.iInterpretSpeciesField == 3) { if ((fieldtype != FTInteger) && (fieldtype != FTDouble)) { DisplayAndLog("Can't import field '%hs' as an integer, it is type %d.", pszFieldName, fieldtype); return false; } } else { if (fieldtype != FTString) { DisplayAndLog("Can't import field '%hs' as a string, it is type %d.", pszFieldName, fieldtype); return false; } } } // Set projection SetProjection(proj); // Initialize arrays m_pSet->Reserve(nElem); // Read Points from SHP and intepret fields SHPObject *psShape; const char *str; int biotype; vtBioType *pBioType; DPoint2 pos; int unfound = 0; for (int i = 0; i < nElem; i++) { // Get the i-th Point in the SHP file psShape = SHPReadObject(hSHP, i); pos.x = psShape->padfX[0]; pos.y = psShape->padfY[0]; SHPDestroyObject(psShape); // Read DBF Attributes per point int species_id = -1; if (opt.bFixedSpecies) species_id = pSpeciesList->GetSpeciesIdByName(opt.strFixedSpeciesName.mb_str(wxConvUTF8)); else { switch (opt.iInterpretSpeciesField) { case 0: species_id = DBFReadIntegerAttribute(db, i, opt.iSpeciesFieldIndex); break; case 1: str = DBFReadStringAttribute(db, i, opt.iSpeciesFieldIndex); species_id = pSpeciesList->GetSpeciesIdByName(str); if (species_id == -1) unfound++; break; case 2: str = DBFReadStringAttribute(db, i, opt.iSpeciesFieldIndex); species_id = pSpeciesList->GetSpeciesIdByCommonName(str); if (species_id == -1) unfound++; break; case 3: biotype = DBFReadIntegerAttribute(db, i, opt.iSpeciesFieldIndex); pBioType = pBioRegion->GetBioType(biotype); if (pBioType) species_id = pBioType->GetWeightedRandomPlant(); break; case 4: str = DBFReadStringAttribute(db, i, opt.iSpeciesFieldIndex); biotype = pBioRegion->FindBiotypeIdByName(str); pBioType = pBioRegion->GetBioType(biotype); if (pBioType) species_id = pBioType->GetWeightedRandomPlant(); break; } } // Make sure we have a valid species if (species_id == -1) continue; vtPlantSpecies *pSpecies = pSpeciesList->GetSpecies(species_id); if (!pSpecies) continue; // Set height float size; if (opt.bHeightRandom) size = random(pSpecies->GetMaxHeight()); else if (opt.iHeightFieldIndex != -1) size = (float) DBFReadDoubleAttribute(db, i, opt.iHeightFieldIndex); else // fixed height size = opt.fHeightFixed; // If we get here, there is a valid plant to append GetPIA()->AddPlant(pos, size, species_id); } if (unfound) DisplayAndLog("Couldn't find species for %d out of %d instances.", unfound, nElem); else DisplayAndLog("Imported %d plant instances.", nElem); DBFClose(db); SHPClose(hSHP); return true; }
/** * Extract data from a SHP/DBF file and intepret it as a vegetation layer. * This produces a single-valued polygonal coverage. * * 'iField' is the index of the field from which to pull the single value. * 'datatype' is either 0, 1, or 2 for whether the indicated field should be * intepreted as a density value (double), the name of a biotype * (string), or the ID of a biotype (int). */ bool vtVegLayer::AddElementsFromSHP_Polys(const wxString &filename, const vtProjection &proj, int iField, VegImportFieldType datatype) { // When working with float field data, must use C locale ScopedLocale normal_numbers(LC_NUMERIC, "C"); // SHPOpen doesn't yet support utf-8 or wide filenames, so convert vtString fname_local = UTF8ToLocal(filename.mb_str(wxConvUTF8)); // Open the SHP File SHPHandle hSHP = SHPOpen(fname_local, "rb"); if (hSHP == NULL) return false; // Get number of polys and type of data int nElem; int nShapeType; SHPGetInfo(hSHP, &nElem, &nShapeType, NULL, NULL); // Check Shape Type, Veg Layer should be Poly data if (nShapeType != SHPT_POLYGON) return false; // Open DBF File DBFHandle db = DBFOpen(fname_local, "rb"); if (db == NULL) return false; // Check for field of poly id, current default field in dbf is Id int *pnWidth = 0, *pnDecimals = 0; char *pszFieldName = NULL; DBFFieldType fieldtype = DBFGetFieldInfo(db, iField, pszFieldName, pnWidth, pnDecimals ); if (datatype == VIFT_Density) { if (fieldtype != FTDouble) { VTLOG(" Expected the DBF field '%s' to be of type 'Double', but found '%s' instead.\n", pszFieldName, DescribeFieldType(fieldtype)); return false; } } if (datatype == VIFT_BiotypeName) { if (fieldtype != FTString) { VTLOG(" Expected the DBF field '%s' to be of type 'String', but found '%s' instead.\n", pszFieldName, DescribeFieldType(fieldtype)); return false; } } if (datatype == VIFT_BiotypeID) { if (fieldtype != FTInteger) { VTLOG(" Expected the DBF field '%s' to be of type 'Integer', but found '%s' instead.\n", pszFieldName, DescribeFieldType(fieldtype)); return false; } } // OK, ready to allocate our featureset if (datatype == VIFT_Density) { SetVegType(VLT_Density); m_field_density = m_pSet->AddField("Density", FT_Float); } if (datatype == VIFT_BiotypeName || datatype == VIFT_BiotypeID) { SetVegType(VLT_BioMap); m_field_biotype = m_pSet->AddField("Biotype", FT_Integer); } SetProjection(proj); // Read Polys from SHP into Veg Poly m_pSet->LoadGeomFromSHP(hSHP); SHPClose(hSHP); // Read fields int biotype_id; for (uint i = 0; i < (uint) nElem; i++) { int record = m_pSet->AddRecord(); // Read DBF Attributes per poly if (datatype == VIFT_Density) { // density m_pSet->SetValue(record, m_field_density, (float) DBFReadDoubleAttribute(db, i, iField)); } if (datatype == VIFT_BiotypeName) { const char *str = DBFReadStringAttribute(db, i, iField); biotype_id = g_bld->GetBioRegion()->FindBiotypeIdByName(str); m_pSet->SetValue(record, m_field_biotype, biotype_id); } if (datatype == VIFT_BiotypeID) { biotype_id = DBFReadIntegerAttribute(db, i, iField); m_pSet->SetValue(record, m_field_biotype, biotype_id); } } DBFClose(db); return true; }
bool vtVegLayer::OnLoad() { vtSpeciesList *plants = g_bld->GetSpeciesList(); if (plants->NumSpecies() == 0) { DisplayAndLog(_("You must specify a species file (plant list) to use when working with vegetation files.")); return false; } wxString fname = GetLayerFilename(); wxString ext = fname.Right(3); wxString dbfname = fname.Left(fname.Length() - 4); dbfname += _T(".dbf"); if (!ext.CmpNoCase(_T(".vf"))) { // read VF file SetVegType(VLT_Instances); GetPIA()->SetSpeciesList(plants); if (GetPIA()->ReadVF(fname.mb_str(wxConvUTF8))) { m_pSet->SetFilename((const char *)fname.mb_str(wxConvUTF8)); return true; } else { delete m_pSet; m_pSet = NULL; m_VLType = VLT_None; return false; } } else if (!ext.CmpNoCase(_T("shp"))) { // SHPOpen doesn't yet support utf-8 or wide filenames, so convert vtString fname_local = UTF8ToLocal(fname.mb_str(wxConvUTF8)); // Study this SHP file, look at what it might be int nElems, nShapeType; SHPHandle hSHP = SHPOpen(fname_local, "rb"); if (hSHP == NULL) return false; SHPGetInfo(hSHP, &nElems, &nShapeType, NULL, NULL); SHPClose(hSHP); if (nShapeType == SHPT_POINT) SetVegType(VLT_Instances); else if (nShapeType == SHPT_POLYGON) { // DBFOpen doesn't yet support utf-8 or wide filenames, so convert vtString fname_localdbf = UTF8ToLocal(dbfname.mb_str(wxConvUTF8)); DBFHandle db = DBFOpen(fname_localdbf, "rb"); if (db == NULL) return false; m_field_density = DBFGetFieldIndex(db, "Density"); m_field_biotype = DBFGetFieldIndex(db, "Biotype"); DBFClose(db); if (m_field_density != -1) SetVegType(VLT_Density); else if (m_field_biotype != -1) SetVegType(VLT_BioMap); else return false; } // OK, read the rest of the file return m_pSet->LoadFromSHP(fname.mb_str(wxConvUTF8)); } // don't know this file return false; }
wxString Internat::FromFilename(const wxString &s) { return UTF8ToLocal(s); }