File: NodingTestUtil.cpp

package info (click to toggle)
geos 3.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,212 kB
  • sloc: cpp: 199,103; xml: 56,065; ansic: 6,162; sh: 287; makefile: 26
file content (114 lines) | stat: -rw-r--r-- 3,631 bytes parent folder | download | duplicates (2)
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
/**********************************************************************
 *
 * GEOS - Geometry Engine Open Source
 * http://geos.osgeo.org
 *
 * Copyright (C) 2020 Paul Ramsey <pramsey@cleverelephant.ca>
 *
 * 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 <util/NodingTestUtil.h>


using namespace geos::geom;
using namespace geos::noding;
using geos::noding::Noder;
using geos::io::WKTReader;
using geos::io::WKTWriter;
using geos::geom::util::LinearComponentExtracter;

namespace geos {

/*private static*/
std::unique_ptr<Geometry>
NodingTestUtil::toLines(const std::vector<SegmentString*>* nodedList, const GeometryFactory* geomFact)
{
    std::vector<std::unique_ptr<Geometry>> lines;

    for (auto nss : *nodedList) {
      CoordinateSequence* pts = nss->getCoordinates();
      // pts is owned by nss, so we make a copy to build the line
      // on top of. Lines are 100% self-contained and own all their parts.
      // Input nodedList can be freed.
      lines.emplace_back(geomFact->createLineString(pts->clone()));
    }
    if (lines.size() == 1) return std::move(lines[0]);

    // move the lines to pass ownership to the multiLineString
    return geomFact->createMultiLineString(std::move(lines));
}


/*private static*/
std::vector<SegmentString*>
NodingTestUtil::toSegmentStrings(std::vector<const LineString*>& lines)
{
    std::vector<SegmentString*> nssList;

    bool constructZ = false;
    bool constructM = false;
    for (auto line : lines) {
        constructZ |= line->hasZ();
        constructM |= line->hasM();
    }

    for (auto line : lines) {
        // line->getCoordinates() clones CoordinateSequence
        // into a unique_ptr<> which we have to release() to the
        // NodedSegmentString constructor, so
        // nss now owns nss->pts
        NodedSegmentString* nss = new NodedSegmentString(line->getCoordinates().release(), constructZ, constructM, line);
        nssList.push_back(nss);
    }
    return nssList;
}

/*public static*/
std::unique_ptr<Geometry>
NodingTestUtil::nodeValidated(const Geometry* geom1, const Geometry* geom2, Noder* noder)
{
    std::vector<const LineString*> lines;
    // lines are const* to linear components of geom1, geom1 still
    // owns all coordinates, etc
    LinearComponentExtracter::getLines(*geom1, lines);
    if (geom2 != nullptr) {
        LinearComponentExtracter::getLines(*geom2, lines);
    }

    // ssList needs to be disposed after noder is done working
    std::vector<SegmentString*> ssList = toSegmentStrings(lines);

    ValidatingNoder noderValid(*noder);
    // computeNotes might alter ssList, but ssList still
    // holds all memory
    noderValid.computeNodes(&ssList);

    // getNodedSubstrings calls NodedSegmentString::getNodedSubStrings()
    // which creates new NodedSegmentString and new pts member, so complete
    // new copy of data. Can be disposed of after geometries are constructed
    // std::vector<SegmentString*>* nodedList = noderValid.getNodedSubstrings();
    std::vector<SegmentString*>* nodedList = noderValid.getNodedSubstrings();

    // Dispose of ssList
    for (auto ss: ssList) {
        delete ss;
    }

    std::unique_ptr<Geometry> lineGeom = toLines(nodedList, geom1->getFactory());

    // Dispose of nodedList
    for (auto nss: *nodedList) {
        delete nss;
    }
    delete nodedList;

    return lineGeom;
}

} // geos