I have a map view in my application, and i want to draw an area of actuation based on the coordinates, for example: point A(x1,y1) to point B(x2,y2) to point C(x3, y3) to point D(x4, y4) and close on point A. I need to convert the distance between the points to meters, because each row and column of the matrix must have a distance of 1 meter on the world map scale between each one. If the X Y of the matrix is inside the drawn area on the map, get value = 1, else, get value = 0.
In this code I set an approximate value of 1 meter in real life, but this makes the result inaccurate, i have to fix it.
if (selectedCoordinates.isEmpty()) {
return;
}
// Get the bounding box of the selected area
double minLat = std::numeric_limits<double>::max();
double maxLat = std::numeric_limits<double>::lowest();
double minLon = std::numeric_limits<double>::max();
double maxLon = std::numeric_limits<double>::lowest();
for (const QVariant &point : selectedCoordinates) {
QVariantMap pointMap = point.toMap();
double lat = pointMap["latitude"].toDouble();
double lon = pointMap["longitude"].toDouble();
if (lat < minLat) minLat = lat;
if (lat > maxLat) maxLat = lat;
if (lon < minLon) minLon = lon;
if (lon > maxLon) maxLon = lon;
}
// Initialize the matrix
QVector<QVector<int>> matrix;
// Calculate the matrix of 1x1 meter squares within the bounding box
double latStep = 1.0 / 111320.0; // Approx. 1 meter per degree latitude
double lonStep = 1.0 / (111320.0 * std::cos(minLat * M_PI / 180.0)); // Approx. 1 meter per degree longitude
for (double lat = minLat; lat <= maxLat; lat += latStep) {
QVector<int> row;
QVariantList rowV;
for (double lon = minLon; lon <= maxLon; lon += lonStep) {
QGeoCoordinate center(lat + latStep / 2, lon + lonStep / 2);
// Check if the center is inside the selection polygon
bool insidePolygon = isInsidePolygon(center);
// Store 1 for inside, 0 for outside
if (insidePolygon) {
row.append(1);
} else {
row.append(0);
}
// Store the coordinates of the center of each square in the matrix
QVariantMap point;
point["latitude"] = center.latitude();
point["longitude"] = center.longitude();
point["inside"] = insidePolygon;
rowV.append(point);
// Draw the square on the map
auto Obje = ui->quickWidget_MapView->rootObject();
if (Obje) {
QVariant returnedValue;
QMetaObject::invokeMethod(Obje, "drawSquare", Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QVariant, QVariant::fromValue(center)),
Q_ARG(QVariant, 1.0),
Q_ARG(QVariant, insidePolygon));
}
}
matrixV.append(rowV);
matrix.append(row);
}
// Print the matrix in a visually organized format
qDebug() << "Matrix of 1x1 meter squares:";
for (const QVector<int> &row : matrix) {
QString rowStr;
for (int value : row) {
if (value == 1) {
rowStr += QString(YELLOW "[1]" RESET " ");
} else {
rowStr += "[0] ";
}
}
qDebug().noquote() << rowStr.trimmed();
}
In a simple polygon, this works fine:
But in a more complex polygon, this occurs: