summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Nienhüser <nienhueser@kde.org>2016-09-03 14:58:54 (GMT)
committerDennis Nienhüser <nienhueser@kde.org>2016-09-03 14:58:54 (GMT)
commitdc4526e9c65fe71f3e8b73430935e61547b3875b (patch)
tree44ad6963008e823e5a9edb03db7fb3fb412b9328
parent642c4af60534bc8ed7f81e66a75365aac1f8ef8b (diff)
Avoid code duplication
-rw-r--r--tools/osm-simplify/BaseClipper.cpp85
-rw-r--r--tools/osm-simplify/BaseClipper.h48
-rw-r--r--tools/osm-simplify/ShpCoastlineProcessor.cpp4
-rw-r--r--tools/osm-simplify/TinyPlanetProcessor.cpp16
4 files changed, 52 insertions, 101 deletions
diff --git a/tools/osm-simplify/BaseClipper.cpp b/tools/osm-simplify/BaseClipper.cpp
index 01d7d1d..2fc7587 100644
--- a/tools/osm-simplify/BaseClipper.cpp
+++ b/tools/osm-simplify/BaseClipper.cpp
@@ -102,91 +102,6 @@ BaseClipper::BaseClipper() :
}
-
-
-QPolygonF BaseClipper::lineString2Qpolygon(const GeoDataLineString& lineString, bool reverseOrder)
-{
- QPolygonF polygon;
- if(!reverseOrder) {
- foreach (const GeoDataCoordinates& coord, lineString) {
- // Need to flip the Y axis(latitude)
- QPointF point(coord.longitude(), -coord.latitude());
- polygon.append(point);
- }
- } else {
- for(int i = lineString.size()-1; i >= 0; --i) {
- // Need to flip the Y axis(latitude)
- QPointF point(lineString.at(i).longitude(), -lineString.at(i).latitude());
- polygon.append(point);
- }
- }
-
- return polygon;
-}
-
-QPolygonF BaseClipper::linearRing2Qpolygon(const GeoDataLinearRing& linearRing, bool reverseOrder)
-{
- QPolygonF polygon;
-
- if(!reverseOrder) {
- foreach (const GeoDataCoordinates& coord, linearRing) {
- // Need to flip the Y axis(latitude)
- QPointF point(coord.longitude(), -coord.latitude());
- polygon.append(point);
- }
- } else {
- for(int i = linearRing.size()-1; i >= 0; --i) {
- // Need to flip the Y axis(latitude)
- QPointF point(linearRing.at(i).longitude(), -linearRing.at(i).latitude());
- polygon.append(point);
- }
- }
-
- return polygon;
-}
-
-GeoDataLineString BaseClipper::qPolygon2lineString(const QPolygonF& polygon, bool reverseOrder)
-{
- GeoDataLineString lineString;
-
- if(!reverseOrder) {
- foreach (const QPointF& point, polygon) {
- // Flipping back the Y axis
- GeoDataCoordinates coord(point.x(), -point.y());
- lineString.append(coord);
- }
- } else {
- for(int i = polygon.size()-1; i >= 0; --i) {
- // Need to flip the Y axis(latitude)
- GeoDataCoordinates coord(polygon.at(i).x(), -polygon.at(i).y());
- lineString.append(coord);
- }
- }
-
- return lineString;
-}
-
-GeoDataLinearRing BaseClipper::qPolygon2linearRing(const QPolygonF& polygon, bool reverseOrder)
-{
- GeoDataLinearRing linearRing;
-
- if(!reverseOrder) {
- foreach (const QPointF& point, polygon) {
- // Flipping back the Y axis
- GeoDataCoordinates coord(point.x(), -point.y());
- linearRing.append(coord);
- }
- } else {
- for(int i = polygon.size()-1; i >= 0; --i) {
- // Need to flip the Y axis(latitude)
- GeoDataCoordinates coord(polygon.at(i).x(), -polygon.at(i).y());
- linearRing.append(coord);
- }
- }
-
- return linearRing;
-}
-
qreal BaseClipper::tileX2lon( unsigned int x, unsigned int maxTileX )
{
return ( (2*M_PI * x) / maxTileX - M_PI );
diff --git a/tools/osm-simplify/BaseClipper.h b/tools/osm-simplify/BaseClipper.h
index 3967143..068f8d6 100644
--- a/tools/osm-simplify/BaseClipper.h
+++ b/tools/osm-simplify/BaseClipper.h
@@ -29,12 +29,48 @@ public:
static qreal tileX2lon( unsigned int x, unsigned int maxTileX );
static qreal tileY2lat( unsigned int y, unsigned int maxTileY );
- static QPolygonF lineString2Qpolygon(const GeoDataLineString &lineString, bool reverseOrder = false);
- static QPolygonF linearRing2Qpolygon(const GeoDataLinearRing &linearRing, bool reverseOrder = false);
-
- static GeoDataLineString qPolygon2lineString(const QPolygonF& polygon, bool reverseOrder = false);
- static GeoDataLinearRing qPolygon2linearRing(const QPolygonF& polygon, bool reverseOrder = false);
-
+ template<class T>
+ static QPolygonF toQPolygon(const T &lineString, bool reverseOrder = false)
+ {
+ QPolygonF polygon;
+ if(!reverseOrder) {
+ foreach (const GeoDataCoordinates& coord, lineString) {
+ // Need to flip the Y axis(latitude)
+ QPointF point(coord.longitude(), -coord.latitude());
+ polygon.append(point);
+ }
+ } else {
+ for(int i = lineString.size()-1; i >= 0; --i) {
+ // Need to flip the Y axis(latitude)
+ QPointF point(lineString.at(i).longitude(), -lineString.at(i).latitude());
+ polygon.append(point);
+ }
+ }
+
+ return polygon;
+ }
+
+ template<class T>
+ static T toLineString(const QPolygonF& polygon, bool reverseOrder = false)
+ {
+ T lineString;
+
+ if(!reverseOrder) {
+ foreach (const QPointF& point, polygon) {
+ // Flipping back the Y axis
+ GeoDataCoordinates coord(point.x(), -point.y());
+ lineString.append(coord);
+ }
+ } else {
+ for(int i = polygon.size()-1; i >= 0; --i) {
+ // Need to flip the Y axis(latitude)
+ GeoDataCoordinates coord(polygon.at(i).x(), -polygon.at(i).y());
+ lineString.append(coord);
+ }
+ }
+
+ return lineString;
+ }
void initClipRect(const GeoDataLatLonBox& clippingBox, int pointsToAddAtEdges);
diff --git a/tools/osm-simplify/ShpCoastlineProcessor.cpp b/tools/osm-simplify/ShpCoastlineProcessor.cpp
index ecf94fe..e93b997 100644
--- a/tools/osm-simplify/ShpCoastlineProcessor.cpp
+++ b/tools/osm-simplify/ShpCoastlineProcessor.cpp
@@ -68,7 +68,7 @@ GeoDataDocument *ShpCoastlineProcessor::cutToTiles(unsigned int zoomLevel, unsig
QVector<QPolygonF> clippedPolygons;
- QPolygonF outerBoundary = BaseClipper::linearRing2Qpolygon(marblePolygon->outerBoundary());
+ QPolygonF outerBoundary = BaseClipper::toQPolygon(marblePolygon->outerBoundary());
qDebug() << "Size before:" << outerBoundary.size();
qDebug() << "Clipping...";
@@ -80,7 +80,7 @@ GeoDataDocument *ShpCoastlineProcessor::cutToTiles(unsigned int zoomLevel, unsig
foreach(const QPolygonF& polygon, clippedPolygons) {
qDebug() << polygon.size();
- GeoDataLinearRing outerBoundary = BaseClipper::qPolygon2linearRing(polygon);
+ GeoDataLinearRing outerBoundary = BaseClipper::toLineString<GeoDataLinearRing>(polygon);
GeoDataPolygon* newMarblePolygon = new GeoDataPolygon();
newMarblePolygon->setOuterBoundary(outerBoundary);
diff --git a/tools/osm-simplify/TinyPlanetProcessor.cpp b/tools/osm-simplify/TinyPlanetProcessor.cpp
index 026928e..7cf6901 100644
--- a/tools/osm-simplify/TinyPlanetProcessor.cpp
+++ b/tools/osm-simplify/TinyPlanetProcessor.cpp
@@ -65,7 +65,7 @@ GeoDataDocument *TinyPlanetProcessor::cutToTiles(unsigned int zoomLevel, unsigne
QVector<PolygonPair> newMarblePolygons;
isClockwise = marblePolygon->outerBoundary().isClockwise();
- QPolygonF outerBoundaryPolygon = BaseClipper::linearRing2Qpolygon(marblePolygon->outerBoundary(), !isClockwise);
+ QPolygonF outerBoundaryPolygon = BaseClipper::toQPolygon(marblePolygon->outerBoundary(), !isClockwise);
QVector<QPolygonF> outerBoundaries;
clipper.clipPolyObject(outerBoundaryPolygon, outerBoundaries, true);
@@ -77,7 +77,7 @@ GeoDataDocument *TinyPlanetProcessor::cutToTiles(unsigned int zoomLevel, unsigne
PolygonPair newMarblePolygon = qMakePair(new GeoDataPlacemark(), polygon);
GeoDataPolygon* geometry = new GeoDataPolygon();
- geometry->setOuterBoundary(BaseClipper::qPolygon2linearRing(polygon, !isClockwise));
+ geometry->setOuterBoundary(BaseClipper::toLineString<GeoDataLinearRing>(polygon, !isClockwise));
newMarblePolygon.first->setGeometry(geometry);
copyTags(*placemark, *(newMarblePolygon.first));
@@ -93,7 +93,7 @@ GeoDataDocument *TinyPlanetProcessor::cutToTiles(unsigned int zoomLevel, unsigne
foreach (const GeoDataLinearRing& innerBoundary, marblePolygon->innerBoundaries()) {
++index;
isClockwise = innerBoundary.isClockwise();
- QPolygonF innerBoundaryPolygon = BaseClipper::linearRing2Qpolygon(innerBoundary, !isClockwise);
+ QPolygonF innerBoundaryPolygon = BaseClipper::toQPolygon(innerBoundary, !isClockwise);
QVector<QPolygonF> clippedPolygons;
@@ -104,7 +104,7 @@ GeoDataDocument *TinyPlanetProcessor::cutToTiles(unsigned int zoomLevel, unsigne
foreach (const PolygonPair& newMarblePolygon, newMarblePolygons) {
if(!polygon.intersected(newMarblePolygon.second).isEmpty()) {
GeoDataPolygon* geometry = static_cast<GeoDataPolygon*>(newMarblePolygon.first->geometry());
- geometry->appendInnerBoundary(BaseClipper::qPolygon2linearRing(polygon, !isClockwise));
+ geometry->appendInnerBoundary(BaseClipper::toLineString<GeoDataLinearRing>(polygon, !isClockwise));
OsmObjectManager::initializeOsmData(newMarblePolygon.first);
@@ -132,7 +132,7 @@ GeoDataDocument *TinyPlanetProcessor::cutToTiles(unsigned int zoomLevel, unsigne
QVector<QPolygonF> clippedPolygons;
- QPolygonF way = BaseClipper::lineString2Qpolygon(*marbleWay);
+ QPolygonF way = BaseClipper::toQPolygon(*marbleWay);
clipper.clipPolyObject(way, clippedPolygons, false);
@@ -142,7 +142,7 @@ GeoDataDocument *TinyPlanetProcessor::cutToTiles(unsigned int zoomLevel, unsigne
// qDebug() << polygon.size();
- GeoDataLineString* newMarbleWay = new GeoDataLineString(BaseClipper::qPolygon2lineString(polygon));
+ GeoDataLineString* newMarbleWay = new GeoDataLineString(BaseClipper::toLineString<GeoDataLineString>(polygon));
GeoDataPlacemark* newPlacemark = new GeoDataPlacemark();
newPlacemark->setGeometry(newMarbleWay);
@@ -156,7 +156,7 @@ GeoDataDocument *TinyPlanetProcessor::cutToTiles(unsigned int zoomLevel, unsigne
QVector<QPolygonF> clippedPolygons;
- QPolygonF closedWay = BaseClipper::linearRing2Qpolygon(*marbleClosedWay);
+ QPolygonF closedWay = BaseClipper::toQPolygon(*marbleClosedWay);
clipper.clipPolyObject(closedWay, clippedPolygons, true);
@@ -165,7 +165,7 @@ GeoDataDocument *TinyPlanetProcessor::cutToTiles(unsigned int zoomLevel, unsigne
// qDebug() << polygon.size();
- GeoDataLinearRing* newMarbleWay = new GeoDataLinearRing(BaseClipper::qPolygon2linearRing(polygon));
+ GeoDataLinearRing* newMarbleWay = new GeoDataLinearRing(BaseClipper::toLineString<GeoDataLinearRing>(polygon));
GeoDataPlacemark* newPlacemark = new GeoDataPlacemark();
newPlacemark->setGeometry(newMarbleWay);