1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2015 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: Leif Delgass <ldelgass@purdue.edu> |
---|
6 | */ |
---|
7 | |
---|
8 | #include <string> |
---|
9 | #include <fstream> |
---|
10 | |
---|
11 | #include <osgDB/FileUtils> |
---|
12 | #include <osgDB/FileNameUtils> |
---|
13 | |
---|
14 | #include "Util.h" |
---|
15 | #include "Trace.h" |
---|
16 | |
---|
17 | using namespace GeoVis; |
---|
18 | |
---|
19 | std::string GeoVis::loadCSVLayer(const char *csvFilePath, const char *longitudeColName, const char *latitudeColName) |
---|
20 | { |
---|
21 | std::string baseName = osgDB::getStrippedName(csvFilePath); |
---|
22 | std::string vrtFile = osgDB::getNameLessExtension(csvFilePath) + std::string(".vrt"); |
---|
23 | if (!osgDB::fileExists(vrtFile)) { |
---|
24 | std::ofstream ofs(vrtFile.c_str()); |
---|
25 | if (ofs.is_open()) { |
---|
26 | // GDAL/OGR < 1.9.2 doesn't like the <?xml?> declaration |
---|
27 | // << "<?xml version=\"1.0\"?>" << std::endl |
---|
28 | ofs << "<OGRVRTDataSource>" << std::endl |
---|
29 | << " <OGRVRTLayer name=\"" << baseName << "\">" << std::endl |
---|
30 | << " <SrcDataSource>" << csvFilePath << "</SrcDataSource>" << std::endl |
---|
31 | << " <GeometryType>wkbPoint</GeometryType>" << std::endl |
---|
32 | << " <LayerSRS>WGS84</LayerSRS>" << std::endl |
---|
33 | << " <GeometryField encoding=\"PointFromColumns\" x=\"" |
---|
34 | << longitudeColName << "\" y=\"" << latitudeColName << "\"/>" << std::endl |
---|
35 | << " </OGRVRTLayer>" << std::endl |
---|
36 | << "</OGRVRTDataSource>" << std::endl; |
---|
37 | ofs.close(); |
---|
38 | } else { |
---|
39 | ERROR("Failed to open file '%s' for writing", vrtFile.c_str()); |
---|
40 | } |
---|
41 | } else { |
---|
42 | TRACE("Found existing file '%s'", vrtFile.c_str()); |
---|
43 | } |
---|
44 | return vrtFile; |
---|
45 | } |
---|