void tst_QGeoCircle::radius() { QGeoCircle c; c.setRadius(1.0); QCOMPARE(c.radius(), qreal(1.0)); c.setRadius(5.0); QCOMPARE(c.radius(), qreal(5.0)); }
void tst_QGeoCircle::assignment() { QGeoCircle c1 = QGeoCircle(QGeoCoordinate(10.0, 0.0), 20.0); QGeoCircle c2 = QGeoCircle(QGeoCoordinate(20.0, 0.0), 30.0); QVERIFY(c1 != c2); c2 = c1; QCOMPARE(c2.center(), QGeoCoordinate(10.0, 0.0)); QCOMPARE(c2.radius(), 20.0); QCOMPARE(c1, c2); c2.setCenter(QGeoCoordinate(30.0, 0.0)); c2.setRadius(15.0); QCOMPARE(c1.center(), QGeoCoordinate(10.0, 0.0)); QCOMPARE(c1.radius(), 20.0); // Assign c1 to an area QGeoShape area = c1; QCOMPARE(area.type(), c1.type()); QVERIFY(area == c1); // Assign the area back to a bounding circle QGeoCircle ca = area; QCOMPARE(ca.center(), c1.center()); QCOMPARE(ca.radius(), c1.radius()); // Check that the copy is not modified when modifying the original. c1.setCenter(QGeoCoordinate(15.0, 15.0)); QVERIFY(ca.center() != c1.center()); QVERIFY(ca != c1); }
void QDeclarativeGeoMap::fitViewportToGeoShape() { double bboxWidth; double bboxHeight; QGeoCoordinate centerCoordinate; switch (m_region.type()) { case QGeoShape::RectangleType: { QGeoRectangle rect = m_region; QDoubleVector2D topLeftPoint = m_map->coordinateToItemPosition(rect.topLeft(), false); QDoubleVector2D botRightPoint = m_map->coordinateToItemPosition(rect.bottomRight(), false); bboxWidth = qAbs(topLeftPoint.x() - botRightPoint.x()); bboxHeight = qAbs(topLeftPoint.y() - botRightPoint.y()); centerCoordinate = rect.center(); break; } case QGeoShape::CircleType: { QGeoCircle circle = m_region; centerCoordinate = circle.center(); QGeoCoordinate edge = centerCoordinate.atDistanceAndAzimuth(circle.radius(), 90); QDoubleVector2D centerPoint = m_map->coordinateToItemPosition(centerCoordinate, false); QDoubleVector2D edgePoint = m_map->coordinateToItemPosition(edge, false); bboxWidth = qAbs(centerPoint.x() - edgePoint.x()) * 2; bboxHeight = bboxWidth; break; } case QGeoShape::UnknownType: //Fallthrough to default default: return; } // position camera to the center of bounding box setProperty("center", QVariant::fromValue(centerCoordinate)); //If the shape is empty we just change centerposition, not zoom if (bboxHeight == 0 && bboxWidth == 0) return; // adjust zoom double bboxWidthRatio = bboxWidth / (bboxWidth + bboxHeight); double mapWidthRatio = width() / (width() + height()); double zoomRatio; if (bboxWidthRatio > mapWidthRatio) zoomRatio = bboxWidth / width(); else zoomRatio = bboxHeight / height(); qreal newZoom = std::log10(zoomRatio) / std::log10(0.5); newZoom = std::floor(qMax(minimumZoomLevel(), (m_map->mapController()->zoom() + newZoom))); setProperty("zoomLevel", QVariant::fromValue(newZoom)); }
void GeoCircleValueType::setRadius(qreal radius) { QGeoCircle c = v; if (c.radius() == radius) return; c.setRadius(radius); v = c; }
QString GeoCircleValueType::toString() const { if (v.type() != QGeoShape::CircleType) { qWarning("Not a circle"); return QStringLiteral("QGeoCircle(not a circle)"); } QGeoCircle c = v; return QStringLiteral("QGeoCircle({%1, %2}, %3)") .arg(c.center().latitude()) .arg(c.center().longitude()) .arg(c.radius()); }
QDataStream &operator<<(QDataStream &stream, const QGeoShape &shape) { stream << quint32(shape.type()); switch (shape.type()) { case QGeoShape::UnknownType: break; case QGeoShape::RectangleType: { QGeoRectangle r = shape; stream << r.topLeft() << r.bottomRight(); break; } case QGeoShape::CircleType: { QGeoCircle c = shape; stream << c.center() << c.radius(); break; } } return stream; }
void QDeclarativeGeoMap::fitViewportToGeoShape() { int margins = 10; if (!m_map || width() <= margins || height() <= margins) return; QGeoCoordinate topLeft; QGeoCoordinate bottomRight; switch (m_region.type()) { case QGeoShape::RectangleType: { QGeoRectangle rect = m_region; topLeft = rect.topLeft(); bottomRight = rect.bottomRight(); break; } case QGeoShape::CircleType: { const double pi = M_PI; QGeoCircle circle = m_region; QGeoCoordinate centerCoordinate = circle.center(); // calculate geo bounding box of the circle // circle tangential points with meridians and the north pole create // spherical triangle, we use spherical law of sines // sin(lon_delta_in_rad)/sin(r_in_rad) = // sin(alpha_in_rad)/sin(pi/2 - lat_in_rad), where: // * lon_delta_in_rad - delta of longitudes of circle center // and tangential points // * r_in_rad - angular radius of the circle // * lat_in_rad - latitude of circle center // * alpha_in_rad - angle between meridian and radius to the circle => // this is tangential point => sin(alpha) = 1 // * lat_delta_in_rad - delta of latitudes of circle center and // latitude of points where great circle (going through circle // center) crosses circle and the pole double r_in_rad = circle.radius() / EARTH_MEAN_RADIUS; // angular r double lat_delta_in_deg = r_in_rad * 180 / pi; double lon_delta_in_deg = std::asin(std::sin(r_in_rad) / std::cos(centerCoordinate.latitude() * pi / 180)) * 180 / pi; topLeft.setLatitude(centerCoordinate.latitude() + lat_delta_in_deg); topLeft.setLongitude(centerCoordinate.longitude() - lon_delta_in_deg); bottomRight.setLatitude(centerCoordinate.latitude() - lat_delta_in_deg); bottomRight.setLongitude(centerCoordinate.longitude() + lon_delta_in_deg); // adjust if circle reaches poles => cross all meridians and // fit into Mercator projection bounds if (topLeft.latitude() > 90 || bottomRight.latitude() < -90) { topLeft.setLatitude(qMin(topLeft.latitude(), 85.05113)); topLeft.setLongitude(-180.0); bottomRight.setLatitude(qMax(bottomRight.latitude(), -85.05113)); bottomRight.setLongitude(180.0); } break; } case QGeoShape::UnknownType: //Fallthrough to default default: return; } // adjust zoom, use reference world to keep things simple // otherwise we would need to do the error prone longitudes // wrapping QDoubleVector2D topLeftPoint = m_map->referenceCoordinateToItemPosition(topLeft); QDoubleVector2D bottomRightPoint = m_map->referenceCoordinateToItemPosition(bottomRight); double bboxWidth = bottomRightPoint.x() - topLeftPoint.x(); double bboxHeight = bottomRightPoint.y() - topLeftPoint.y(); // find center of the bounding box QGeoCoordinate centerCoordinate = m_map->referenceItemPositionToCoordinate( (topLeftPoint + bottomRightPoint)/2); // position camera to the center of bounding box setCenter(centerCoordinate); // if the shape is empty we just change center position, not zoom if (bboxHeight == 0 && bboxWidth == 0) return; double zoomRatio = qMax(bboxWidth / (width() - margins), bboxHeight / (height() - margins)); // fixme: use log2 with c++11 zoomRatio = std::log(zoomRatio) / std::log(2.0); double newZoom = qMax((double)minimumZoomLevel(), m_map->mapController()->zoom() - zoomRatio); setZoomLevel(newZoom); m_validRegion = true; }
/*! \qmlmethod QtLocation::Map::fitViewportToGeoShape(QGeoShape shape) Fits the current viewport to the boundary of the shape. The camera is positioned in the center of the shape, and at the largest integral zoom level possible which allows the whole shape to be visible on screen */ void QDeclarativeGeoMap::fitViewportToGeoShape(const QVariant &variantShape) { if (!map_ || !mappingManagerInitialized_) return; QGeoShape shape; if (variantShape.userType() == qMetaTypeId<QGeoRectangle>()) shape = variantShape.value<QGeoRectangle>(); else if (variantShape.userType() == qMetaTypeId<QGeoCircle>()) shape = variantShape.value<QGeoCircle>(); else if (variantShape.userType() == qMetaTypeId<QGeoShape>()) shape = variantShape.value<QGeoShape>(); if (!shape.isValid()) return; double bboxWidth; double bboxHeight; QGeoCoordinate centerCoordinate; switch (shape.type()) { case QGeoShape::RectangleType: { QGeoRectangle rect = shape; QDoubleVector2D topLeftPoint = map_->coordinateToScreenPosition(rect.topLeft(), false); QDoubleVector2D botRightPoint = map_->coordinateToScreenPosition(rect.bottomRight(), false); bboxWidth = qAbs(topLeftPoint.x() - botRightPoint.x()); bboxHeight = qAbs(topLeftPoint.y() - botRightPoint.y()); centerCoordinate = rect.center(); break; } case QGeoShape::CircleType: { QGeoCircle circle = shape; centerCoordinate = circle.center(); QGeoCoordinate edge = centerCoordinate.atDistanceAndAzimuth(circle.radius(), 90); QDoubleVector2D centerPoint = map_->coordinateToScreenPosition(centerCoordinate, false); QDoubleVector2D edgePoint = map_->coordinateToScreenPosition(edge, false); bboxWidth = qAbs(centerPoint.x() - edgePoint.x()) * 2; bboxHeight = bboxWidth; break; } case QGeoShape::UnknownType: //Fallthrough to default default: return; } // position camera to the center of bounding box setProperty("center", QVariant::fromValue(centerCoordinate)); //If the shape is empty we just change centerposition, not zoom if (bboxHeight == 0 && bboxWidth == 0) return; // adjust zoom double bboxWidthRatio = bboxWidth / (bboxWidth + bboxHeight); double mapWidthRatio = width() / (width() + height()); double zoomRatio; if (bboxWidthRatio > mapWidthRatio) zoomRatio = bboxWidth / width(); else zoomRatio = bboxHeight / height(); qreal newZoom = log10(zoomRatio) / log10(0.5); newZoom = floor(qMax(minimumZoomLevel(), (map_->mapController()->zoom() + newZoom))); setProperty("zoomLevel", QVariant::fromValue(newZoom)); }
void tst_QGeoCircle::defaultConstructor() { QGeoCircle c; QVERIFY(!c.center().isValid()); QCOMPARE(c.radius(), qreal(-1.0)); }