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
|
/**********************************************************************
*
* 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 "WKTFileReader.h"
using namespace geos::geom;
WKTFileReader::WKTFileReader()
{
}
WKTFileReader::~WKTFileReader() {
}
/*public*/
std::vector<Geometry*>
WKTFileReader::read(std::string fname)
{
std::ifstream f( fname );
std::vector<Geometry*> geoms;
geos::io::WKTReader rdr;
while (true) {
auto g = readGeom( f, rdr );
if (g == nullptr) {
break;
}
geoms.push_back(g);
}
f.close();
return geoms;
}
/*
Return: nullptr if at EOF
*/
Geometry*
WKTFileReader::readGeom(std::ifstream& f, geos::io::WKTReader& rdr)
{
std::string wkt = "";
int lParen = 0;
int 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.release();
}
|