File: csv2kml.cc

package info (click to toggle)
libkml 1.3.0-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,660 kB
  • sloc: cpp: 48,088; python: 2,008; xml: 1,806; ansic: 1,766; php: 223; java: 195; ruby: 109; perl: 108; sh: 42; makefile: 17
file content (118 lines) | stat: -rw-r--r-- 4,645 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2009, Google Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//  1. Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//  2. Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//  3. Neither the name of Google Inc. nor the names of its contributors may be
//     used to endorse or promote products derived from this software without
//     specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// This creates a KML file from a CSV file whose first row is a schema such as:
//  name,latitude,longitude,description,a,b,c
// And each of whose lines look like:
//  hello,37.1,-111.123,how are you,1,2,3
// A Point Placemark is created for each line in the CSV file.  This example
// prints an error for and drops each line not exactly matching the schema.
// For very very large CSV files see: examples/regionator/csvregionator.cc.
// For very very large KML files see: streamkml.cc.

#include <iostream>
#include <string>
#include "kml/base/csv_splitter.h"
#include "kml/base/file.h"
#include "kml/base/string_util.h"
#include "kml/convenience/csv_parser.h"
#include "kml/convenience/convenience.h"
#include "kml/dom.h"
#include "kml/engine.h"

using kmlbase::File;
using kmlconvenience::CreatePointPlacemark;
using kmlconvenience::CsvParser;
using kmlconvenience::CsvParserHandler;
using kmlconvenience::CsvParserStatus;
using kmldom::ContainerPtr;
using kmldom::FolderPtr;
using kmldom::KmlFactory;
using kmldom::KmlPtr;
using kmldom::PlacemarkPtr;
using kmlengine::KmlFile;
using kmlengine::KmlFilePtr;

// This CsvParserHandler saves all "OK" Placemarks to the passed Container.
class ContainerSaver : public CsvParserHandler {
 public:
  ContainerSaver(ContainerPtr container)
    : container_(container) {
  }

  virtual bool HandleLine(int line, CsvParserStatus status, PlacemarkPtr p) {
    if (status == kmlconvenience::CSV_PARSER_STATUS_OK) {
      container_->add_feature(p);
    } else {
      if (line == 1 && status == kmlconvenience::CSV_PARSER_STATUS_NO_LAT_LON) {
        std::cerr << "Schema line must have both \"latitude\" and "
          <<  "\"longitude\"." << std::endl;
      } else {
        std::cerr << "Error on line " << line << std::endl;
      }
    }
    return true;
  }

 private:
  ContainerPtr container_;
};

int main(int argc, char** argv) {
  if (argc != 3) {
    std::cerr << "usage: " << argv[0] << " input.csv output.kml" << std::endl;
    return 1;
  }

  std::string csv_data;
  if (!kmlbase::File::ReadFileToString(argv[1], &csv_data)) {
    std::cerr << "Read failed: " << argv[1] << std::endl;
    return 1;
  }
  kmlbase::CsvSplitter csv_splitter(csv_data);

  // Create a <Folder> and a ContainerSaver to write to it.
  FolderPtr folder = kmldom::KmlFactory::GetFactory()->CreateFolder();
  ContainerSaver container_saver(folder);

  // Call the CsvParser in strict mode to convert each line of CSV data into
  // a Placemark saved into the supplied Folder.
  if (!kmlconvenience::CsvParser::ParseCsv(&csv_splitter, &container_saver)) {
    std::cerr << "ParseCsv failed: " << argv[1] << std::endl;
    return 1;
  }

  std::cout << "Feature count " << folder->get_feature_array_size()
            << std::endl;

  // Import to kmlengine::KmlFile to get a nice xml header and xml namespace on
  // the root element.
  kmldom::KmlPtr kml = kmldom::KmlFactory::GetFactory()->CreateKml();
  kml->set_feature(folder);
  KmlFilePtr kml_file = KmlFile::CreateFromImport(kml);
  std::string xml;
  kml_file->SerializeToString(&xml);
  return File::WriteStringToFile(xml, argv[2]) ? 1 : 0;
}