forked from libgeos/geos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWKTFileReader.cpp
87 lines (69 loc) · 1.77 KB
/
WKTFileReader.cpp
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
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (C) 2020 Martin Davis
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************/
#include <geos/io/WKTReader.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <memory> // for unique_ptr
#include <algorithm>
#include <geos/io/WKTFileReader.h>
using namespace geos::geom;
namespace geos {
namespace io {
WKTFileReader::WKTFileReader()
{
}
WKTFileReader::~WKTFileReader() {
}
/*public*/
std::vector<std::unique_ptr<Geometry>>
WKTFileReader::read(std::string fname)
{
std::ifstream f( fname );
std::vector<std::unique_ptr<Geometry>> geoms;
geos::io::WKTReader rdr;
while (true) {
auto g = readGeom( f, rdr );
if (g == nullptr) {
break;
}
geoms.push_back(std::move(g));
}
f.close();
return geoms;
}
/*
Return: nullptr if at EOF
*/
std::unique_ptr<Geometry>
WKTFileReader::readGeom(std::ifstream& f, geos::io::WKTReader& rdr)
{
std::string wkt = "";
std::string::difference_type lParen = 0;
std::string::difference_type rParen = 0;
do {
std::string line;
std::getline(f, line);
if (! f) {
return nullptr;
}
lParen += std::count(line.begin(), line.end(), '(');
rParen += std::count(line.begin(), line.end(), ')');
wkt += line;
} while (lParen == 0 || lParen != rParen);
auto g = rdr.read( wkt.c_str() );
return g;
}
}
}