/src/geos/src/precision/GeometryPrecisionReducer.cpp
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * GEOS - Geometry Engine Open Source |
4 | | * http://geos.osgeo.org |
5 | | * |
6 | | * Copyright (C) 2005-2006 Refractions Research Inc. |
7 | | * Copyright (C) 2001-2002 Vivid Solutions Inc. |
8 | | * |
9 | | * This is free software; you can redistribute and/or modify it under |
10 | | * the terms of the GNU Lesser General Public Licence as published |
11 | | * by the Free Software Foundation. |
12 | | * See the COPYING file for more information. |
13 | | * |
14 | | *********************************************************************** |
15 | | * |
16 | | * Last port: precision/GeometryPrecisionReducer.cpp rev. 1.10 (JTS-1.7) |
17 | | * |
18 | | **********************************************************************/ |
19 | | |
20 | | #include <geos/precision/GeometryPrecisionReducer.h> |
21 | | #include <geos/geom/util/GeometryEditor.h> |
22 | | #include <geos/geom/util/NoOpGeometryOperation.h> |
23 | | #include <geos/geom/Coordinate.h> |
24 | | #include <geos/geom/CoordinateSequence.h> |
25 | | #include <geos/geom/PrecisionModel.h> |
26 | | #include <geos/geom/GeometryFactory.h> |
27 | | #include <geos/geom/LineString.h> |
28 | | #include <geos/geom/LinearRing.h> |
29 | | #include <geos/operation/overlayng/PrecisionReducer.h> |
30 | | #include <geos/precision/PrecisionReducerTransformer.h> |
31 | | #include <geos/precision/PointwisePrecisionReducerTransformer.h> |
32 | | |
33 | | #include <vector> |
34 | | #include <typeinfo> |
35 | | |
36 | | using namespace geos::geom; |
37 | | using namespace geos::geom::util; |
38 | | |
39 | | namespace geos { |
40 | | namespace precision { // geos.precision |
41 | | |
42 | | |
43 | | |
44 | | /* private */ |
45 | | std::unique_ptr<Geometry> |
46 | | GeometryPrecisionReducer::changePM(const Geometry* geom, const geom::PrecisionModel& newPM) |
47 | 0 | { |
48 | 0 | const GeometryFactory* previousFactory = geom->getFactory(); |
49 | 0 | GeometryFactory::Ptr changedFactory = createFactory(*previousFactory, newPM); |
50 | 0 | GeometryEditor geomEdit(changedFactory.get()); |
51 | | |
52 | | // this operation changes the PM for the entire geometry tree |
53 | 0 | NoOpGeometryOperation noop; |
54 | 0 | return geomEdit.edit(geom, &noop); |
55 | 0 | } |
56 | | |
57 | | |
58 | | /* public static */ |
59 | | std::unique_ptr<Geometry> |
60 | | GeometryPrecisionReducer::reducePointwise(const Geometry& g, const geom::PrecisionModel& precModel) |
61 | 0 | { |
62 | 0 | GeometryPrecisionReducer reducer(precModel); |
63 | 0 | reducer.setPointwise(true); |
64 | 0 | return reducer.reduce(g); |
65 | 0 | } |
66 | | |
67 | | /* public static */ |
68 | | std::unique_ptr<Geometry> |
69 | | GeometryPrecisionReducer::reduceKeepCollapsed(const Geometry& g, const geom::PrecisionModel& precModel) |
70 | 0 | { |
71 | 0 | GeometryPrecisionReducer reducer(precModel); |
72 | 0 | reducer.setRemoveCollapsedComponents(false); |
73 | 0 | return reducer.reduce(g); |
74 | 0 | } |
75 | | |
76 | | |
77 | | /* public static */ |
78 | | std::unique_ptr<geom::Geometry> |
79 | | GeometryPrecisionReducer::reduce(const geom::Geometry& g, const geom::PrecisionModel& precModel) |
80 | 0 | { |
81 | 0 | GeometryPrecisionReducer reducer(precModel); |
82 | 0 | return reducer.reduce(g); |
83 | 0 | } |
84 | | |
85 | | |
86 | | /* private */ |
87 | | GeometryFactory::Ptr |
88 | | GeometryPrecisionReducer::createFactory(const GeometryFactory& oldGF, |
89 | | const PrecisionModel& newPM) |
90 | 0 | { |
91 | 0 | GeometryFactory::Ptr p_newFactory( |
92 | 0 | GeometryFactory::create(&newPM, |
93 | 0 | oldGF.getSRID()) |
94 | 0 | ); |
95 | 0 | return p_newFactory; |
96 | 0 | } |
97 | | |
98 | | /* public */ |
99 | | std::unique_ptr<Geometry> |
100 | | GeometryPrecisionReducer::reduce(const Geometry& geom) |
101 | 0 | { |
102 | 0 | std::unique_ptr<Geometry> reduced; |
103 | 0 | if (isPointwise) { |
104 | 0 | reduced = PointwisePrecisionReducerTransformer::reduce(geom, targetPM); |
105 | 0 | } |
106 | 0 | else { |
107 | 0 | reduced = PrecisionReducerTransformer::reduce(geom, targetPM, removeCollapsed); |
108 | 0 | } |
109 | | |
110 | | // Match the collection level of the output to the input |
111 | | // if necessary |
112 | 0 | if (geom.isCollection() |
113 | 0 | && ! reduced->isCollection() |
114 | 0 | && (geom.getCoordinateDimension() == reduced->getCoordinateDimension())) |
115 | 0 | { |
116 | 0 | reduced = geom.getFactory()->createMulti(std::move(reduced)); |
117 | 0 | } |
118 | | |
119 | | // TODO: incorporate this in the Transformer above |
120 | 0 | if (changePrecisionModel && |
121 | 0 | (&targetPM != geom.getFactory()->getPrecisionModel())) |
122 | 0 | { |
123 | 0 | return changePM(reduced.get(), targetPM); |
124 | 0 | } |
125 | | |
126 | 0 | return reduced; |
127 | 0 | } |
128 | | |
129 | | |
130 | | } // namespace geos.precision |
131 | | } // namespace geos |