CPLErr Txt2Any(const char * txt, const char * shp, Option opt) { Txt txtfile(txt, opt); std::list<Point> ring = txtfile.getRing(); auto geo = GeometryFromRing(ring); Any shpfile(shp, opt); shpfile.AddGeometry(geo); return CE_None; }
int main(int argc, char** argv) { NcError error(NcError::silent_nonfatal); try { // Input filename std::string strInputFile; // Output mesh filename std::string strOutputFile; // Parse the command line BeginCommandLine() CommandLineString(strInputFile, "in", ""); CommandLineString(strOutputFile, "out", ""); ParseCommandLine(argc, argv); EndCommandLine(argv) // Check file names if (strInputFile == "") { std::cout << "ERROR: No input file specified" << std::endl; return (-1); } if (strOutputFile == "") { std::cout << "ERROR: No output file specified" << std::endl; return (-1); } AnnounceBanner(); // Load shapefile AnnounceStartBlock("Loading shapefile"); std::ifstream shpfile( strInputFile.c_str(), std::ios::in | std::ios::binary); SHPHeader shphead; shpfile.read((char*)(&shphead), sizeof(SHPHeader)); if (O32_HOST_ORDER == O32_LITTLE_ENDIAN) { shphead.iFileCode = SwapEndianInt32(shphead.iFileCode); shphead.iFileLength = SwapEndianInt32(shphead.iFileLength); } else if (O32_HOST_ORDER == O32_BIG_ENDIAN) { shphead.iVersion = SwapEndianInt32(shphead.iVersion); shphead.iShapeType = SwapEndianInt32(shphead.iShapeType); } else { _EXCEPTIONT("Invalid system Endian"); } if (shphead.iFileCode != SHPFileCodeRef) { _EXCEPTIONT("Input file does not appear to be a ESRI Shapefile: " "File code mismatch"); } if (shphead.iVersion != SHPVersionRef) { _EXCEPTIONT("Input file error: Version mismatch"); } if (shphead.iShapeType != SHPPolygonType) { _EXCEPTIONT("Input file error: Polygon type expected"); } SHPBounds shpbounds; shpfile.read((char*)(&shpbounds), sizeof(SHPBounds)); if (O32_HOST_ORDER == O32_BIG_ENDIAN) { shpbounds.dXmin = SwapEndianDouble(shpbounds.dXmin); shpbounds.dYmin = SwapEndianDouble(shpbounds.dYmin); shpbounds.dXmax = SwapEndianDouble(shpbounds.dXmax); shpbounds.dYmax = SwapEndianDouble(shpbounds.dXmax); shpbounds.dZmin = SwapEndianDouble(shpbounds.dZmin); shpbounds.dZmax = SwapEndianDouble(shpbounds.dZmax); shpbounds.dMmin = SwapEndianDouble(shpbounds.dMmin); shpbounds.dMmax = SwapEndianDouble(shpbounds.dMmax); } // Current position (in 16-bit words) int32_t iCurrentPosition = 50; int32_t iPolygonIx = 1; // Exodus mesh Mesh mesh; // Load records while (iCurrentPosition < shphead.iFileLength) { // Read the record header SHPRecordHeader shprechead; shpfile.read((char*)(&shprechead), sizeof(SHPRecordHeader)); if (shpfile.eof()) { break; } if (O32_HOST_ORDER == O32_LITTLE_ENDIAN) { shprechead.iNumber = SwapEndianInt32(shprechead.iNumber); shprechead.nLength = SwapEndianInt32(shprechead.nLength); } char szBuffer[128]; sprintf(szBuffer, "Polygon %i", shprechead.iNumber); iPolygonIx++; AnnounceStartBlock(szBuffer); iCurrentPosition += shprechead.nLength; // Read the shape type int32_t iShapeType; shpfile.read((char*)(&iShapeType), sizeof(int32_t)); if (shpfile.eof()) { break; } if (O32_HOST_ORDER == O32_BIG_ENDIAN) { iShapeType = SwapEndianInt32(iShapeType); } if (iShapeType != SHPPolygonType) { _EXCEPTIONT("Input file error: Record Polygon type expected"); } // Read the polygon header SHPPolygonHeader shppolyhead; shpfile.read((char*)(&shppolyhead), sizeof(SHPPolygonHeader)); if (shpfile.eof()) { break; } if (O32_HOST_ORDER == O32_BIG_ENDIAN) { shppolyhead.dXmin = SwapEndianDouble(shppolyhead.dXmin); shppolyhead.dYmin = SwapEndianDouble(shppolyhead.dYmin); shppolyhead.dXmax = SwapEndianDouble(shppolyhead.dXmax); shppolyhead.dYmax = SwapEndianDouble(shppolyhead.dYmax); shppolyhead.nNumParts = SwapEndianInt32(shppolyhead.nNumParts); shppolyhead.nNumPoints = SwapEndianInt32(shppolyhead.nNumPoints); } // Sanity check if (shppolyhead.nNumParts > 0x1000000) { _EXCEPTION1("Polygon NumParts exceeds sanity bound (%i)", shppolyhead.nNumParts); } if (shppolyhead.nNumPoints > 0x1000000) { _EXCEPTION1("Polygon NumPoints exceeds sanity bound (%i)", shppolyhead.nNumPoints); } Announce("containing %i part(s) with %i points", shppolyhead.nNumParts, shppolyhead.nNumPoints); Announce("Xmin: %3.5f", shppolyhead.dXmin); Announce("Ymin: %3.5f", shppolyhead.dYmin); Announce("Xmax: %3.5f", shppolyhead.dXmax); Announce("Ymax: %3.5f", shppolyhead.dYmax); if (shppolyhead.nNumParts != 1) { _EXCEPTIONT("Only polygons with 1 part currently supported" " in Exodus format"); } DataVector<int32_t> iParts(shppolyhead.nNumParts); shpfile.read((char*)&(iParts[0]), shppolyhead.nNumParts * sizeof(int32_t)); if (shpfile.eof()) { break; } DataVector<double> dPoints(shppolyhead.nNumPoints * 2); shpfile.read((char*)&(dPoints[0]), shppolyhead.nNumPoints * 2 * sizeof(double)); if (shpfile.eof()) { break; } if (O32_HOST_ORDER == O32_BIG_ENDIAN) { for (int i = 0; i < shppolyhead.nNumParts; i++) { iParts[i] = SwapEndianInt32(iParts[i]); } for (int i = 0; i < shppolyhead.nNumPoints * 2; i++) { dPoints[i] = SwapEndianDouble(dPoints[i]); } } // Convert to Exodus mesh int nFaces = mesh.faces.size(); int nNodes = mesh.nodes.size(); mesh.faces.resize(nFaces+1); mesh.nodes.resize(nNodes + shppolyhead.nNumPoints); mesh.faces[nFaces] = Face(shppolyhead.nNumPoints); for (int i = 0; i < shppolyhead.nNumPoints; i++) { double dLonRad = dPoints[2*i] / 180.0 * M_PI; double dLatRad = dPoints[2*i+1] / 180.0 * M_PI; mesh.nodes[nNodes+i].x = cos(dLatRad) * cos(dLonRad); mesh.nodes[nNodes+i].y = cos(dLatRad) * sin(dLonRad); mesh.nodes[nNodes+i].z = sin(dLatRad); mesh.faces[nFaces].SetNode(i, nNodes + i); } AnnounceEndBlock("Done"); } AnnounceEndBlock("Done"); // Write to file AnnounceStartBlock("Write Exodus mesh"); mesh.Write(strOutputFile); AnnounceEndBlock("Done"); // Announce AnnounceBanner(); return (0); } catch(Exception & e) { Announce(e.ToString().c_str()); return (-1); } catch(...) { return (-2); } }