Unverified Commit 4f2b19dd authored by Don Gagne's avatar Don Gagne Committed by GitHub

Merge pull request #7033 from DonLakeFlyer/UTMSHPLoad

Support UTM SHP files
parents 3c45054e 687dde15
...@@ -8,15 +8,22 @@ ...@@ -8,15 +8,22 @@
****************************************************************************/ ****************************************************************************/
#include "SHPFileHelper.h" #include "SHPFileHelper.h"
#include "UTM.h"
#include <QFile> #include <QFile>
#include <QVariant> #include <QVariant>
#include <QtDebug> #include <QtDebug>
#include <QRegularExpression>
const char* SHPFileHelper::_errorPrefix = QT_TR_NOOP("SHP file load failed. %1"); const char* SHPFileHelper::_errorPrefix = QT_TR_NOOP("SHP file load failed. %1");
bool SHPFileHelper::_validateSHPFiles(const QString& shpFile, QString& errorString) /// Validates the specified SHP file is truly a SHP file and is in the format we understand.
/// @param utmZone[out] Zone for UTM shape, 0 for lat/lon shape
/// @param utmSouthernHemisphere[out] true/false for UTM hemisphere
/// @return true: Valid supported SHP file found, false: Invalid or unsupported file found
bool SHPFileHelper::_validateSHPFiles(const QString& shpFile, int* utmZone, bool* utmSouthernHemisphere, QString& errorString)
{ {
*utmZone = 0;
errorString.clear(); errorString.clear();
if (shpFile.endsWith(QStringLiteral(".shp"))) { if (shpFile.endsWith(QStringLiteral(".shp"))) {
...@@ -26,8 +33,25 @@ bool SHPFileHelper::_validateSHPFiles(const QString& shpFile, QString& errorStri ...@@ -26,8 +33,25 @@ bool SHPFileHelper::_validateSHPFiles(const QString& shpFile, QString& errorStri
if (prjFile.open(QIODevice::ReadOnly | QIODevice::Text)) { if (prjFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream strm(&prjFile); QTextStream strm(&prjFile);
QString line = strm.readLine(); QString line = strm.readLine();
if (!line.startsWith(QStringLiteral("GEOGCS[\"GCS_WGS_1984\","))) { if (line.startsWith(QStringLiteral("GEOGCS[\"GCS_WGS_1984\","))) {
errorString = QString(_errorPrefix).arg(tr("Only WGS84 projections are supported.")); *utmZone = 0;
*utmSouthernHemisphere = false;
} else if (line.startsWith(QStringLiteral("PROJCS[\"WGS_1984_UTM_Zone_"))) {
QRegularExpression regEx(QStringLiteral("^PROJCS\\[\"WGS_1984_UTM_Zone_(\\d+){1,2}([NS]{1})"));
QRegularExpressionMatch regExMatch = regEx.match(line);
QStringList rgCapture = regExMatch.capturedTexts();
if (rgCapture.count() == 3) {
int zone = rgCapture[1].toInt();
if (zone >= 1 && zone <= 60) {
*utmZone = zone;
*utmSouthernHemisphere = rgCapture[2] == QStringLiteral("S");
}
}
if (*utmZone == 0) {
errorString = QString(_errorPrefix).arg(tr("UTM projection is not in supported format. Must be PROJCS[\"WGS_1984_UTM_Zone_##N/S"));
}
} else {
errorString = QString(_errorPrefix).arg(tr("Only WGS84 or UTM projections are supported."));
} }
} else { } else {
errorString = QString(_errorPrefix).arg(tr("PRJ file open failed: %1").arg(prjFile.errorString())); errorString = QString(_errorPrefix).arg(tr("PRJ file open failed: %1").arg(prjFile.errorString()));
...@@ -42,13 +66,15 @@ bool SHPFileHelper::_validateSHPFiles(const QString& shpFile, QString& errorStri ...@@ -42,13 +66,15 @@ bool SHPFileHelper::_validateSHPFiles(const QString& shpFile, QString& errorStri
return errorString.isEmpty(); return errorString.isEmpty();
} }
SHPHandle SHPFileHelper::_loadShape(const QString& shpFile, QString& errorString) /// @param utmZone[out] Zone for UTM shape, 0 for lat/lon shape
/// @param utmSouthernHemisphere[out] true/false for UTM hemisphere
SHPHandle SHPFileHelper::_loadShape(const QString& shpFile, int* utmZone, bool* utmSouthernHemisphere, QString& errorString)
{ {
SHPHandle shpHandle = Q_NULLPTR; SHPHandle shpHandle = Q_NULLPTR;
errorString.clear(); errorString.clear();
if (_validateSHPFiles(shpFile, errorString)) { if (_validateSHPFiles(shpFile, utmZone, utmSouthernHemisphere, errorString)) {
if (!(shpHandle = SHPOpen(shpFile.toUtf8(), "rb"))) { if (!(shpHandle = SHPOpen(shpFile.toUtf8(), "rb"))) {
errorString = QString(_errorPrefix).arg(tr("SHPOpen failed.")); errorString = QString(_errorPrefix).arg(tr("SHPOpen failed."));
} }
...@@ -63,7 +89,9 @@ ShapeFileHelper::ShapeType SHPFileHelper::determineShapeType(const QString& shpF ...@@ -63,7 +89,9 @@ ShapeFileHelper::ShapeType SHPFileHelper::determineShapeType(const QString& shpF
errorString.clear(); errorString.clear();
SHPHandle shpHandle = SHPFileHelper::_loadShape(shpFile, errorString); int utmZone;
bool utmSouthernHemisphere;
SHPHandle shpHandle = SHPFileHelper::_loadShape(shpFile, &utmZone, &utmSouthernHemisphere, errorString);
if (errorString.isEmpty()) { if (errorString.isEmpty()) {
int cEntities, type; int cEntities, type;
...@@ -85,6 +113,8 @@ ShapeFileHelper::ShapeType SHPFileHelper::determineShapeType(const QString& shpF ...@@ -85,6 +113,8 @@ ShapeFileHelper::ShapeType SHPFileHelper::determineShapeType(const QString& shpF
bool SHPFileHelper::loadPolygonFromFile(const QString& shpFile, QList<QGeoCoordinate>& vertices, QString& errorString) bool SHPFileHelper::loadPolygonFromFile(const QString& shpFile, QList<QGeoCoordinate>& vertices, QString& errorString)
{ {
int utmZone = 0;
bool utmSouthernHemisphere;
double vertexFilterMeters = 5; double vertexFilterMeters = 5;
SHPHandle shpHandle = Q_NULLPTR; SHPHandle shpHandle = Q_NULLPTR;
SHPObject* shpObject = Q_NULLPTR; SHPObject* shpObject = Q_NULLPTR;
...@@ -92,7 +122,7 @@ bool SHPFileHelper::loadPolygonFromFile(const QString& shpFile, QList<QGeoCoordi ...@@ -92,7 +122,7 @@ bool SHPFileHelper::loadPolygonFromFile(const QString& shpFile, QList<QGeoCoordi
errorString.clear(); errorString.clear();
vertices.clear(); vertices.clear();
shpHandle = SHPFileHelper::_loadShape(shpFile, errorString); shpHandle = SHPFileHelper::_loadShape(shpFile, &utmZone, &utmSouthernHemisphere, errorString);
if (!errorString.isEmpty()) { if (!errorString.isEmpty()) {
goto Error; goto Error;
} }
...@@ -111,7 +141,14 @@ bool SHPFileHelper::loadPolygonFromFile(const QString& shpFile, QList<QGeoCoordi ...@@ -111,7 +141,14 @@ bool SHPFileHelper::loadPolygonFromFile(const QString& shpFile, QList<QGeoCoordi
} }
for (int i=0; i<shpObject->nVertices; i++) { for (int i=0; i<shpObject->nVertices; i++) {
vertices.append(QGeoCoordinate(shpObject->padfY[i], shpObject->padfX[i])); double lat, lon;
if (utmZone) {
UTMXYToLatLon(shpObject->padfX[i], shpObject->padfY[i], utmZone, utmSouthernHemisphere, lat, lon);
} else {
lat = shpObject->padfY[i];
lon = shpObject->padfX[i];
}
vertices.append(QGeoCoordinate(lat, lon));
} }
// Filter last vertex such that it differs from first // Filter last vertex such that it differs from first
......
...@@ -29,8 +29,8 @@ public: ...@@ -29,8 +29,8 @@ public:
static bool loadPolygonFromFile(const QString& shpFile, QList<QGeoCoordinate>& vertices, QString& errorString); static bool loadPolygonFromFile(const QString& shpFile, QList<QGeoCoordinate>& vertices, QString& errorString);
private: private:
static bool _validateSHPFiles(const QString& shpFile, QString& errorString); static bool _validateSHPFiles(const QString& shpFile, int* utmZone, bool* utmSouthernHemisphere, QString& errorString);
static SHPHandle _loadShape(const QString& shpFile, QString& errorString); static SHPHandle _loadShape(const QString& shpFile, int* utmZone, bool* utmSouthernHemisphere, QString& errorString);
static const char* _errorPrefix; static const char* _errorPrefix;
}; };
This diff is collapsed.
...@@ -3,9 +3,6 @@ ...@@ -3,9 +3,6 @@
// Original Javascript by Chuck Taylor // Original Javascript by Chuck Taylor
// Port to C++ by Alex Hajnal // Port to C++ by Alex Hajnal
// //
// *** THIS CODE USES 32-BIT FLOATS BY DEFAULT ***
// *** For 64-bit double-precision edit this file: undefine FLOAT_32 and define FLOAT_64 (see below)
//
// This is a simple port of the code on the Geographic/UTM Coordinate Converter (1) page from Javascript to C++. // This is a simple port of the code on the Geographic/UTM Coordinate Converter (1) page from Javascript to C++.
// Using this you can easily convert between UTM and WGS84 (latitude and longitude). // Using this you can easily convert between UTM and WGS84 (latitude and longitude).
// Accuracy seems to be around 50cm (I suspect rounding errors are limiting precision). // Accuracy seems to be around 50cm (I suspect rounding errors are limiting precision).
...@@ -15,58 +12,18 @@ ...@@ -15,58 +12,18 @@
// //
// 1) http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html // 1) http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html
// QGC Note: This file has been slightly modified to prevent possible conflicts with other parts of the system
#ifndef UTM_H #ifndef UTM_H
#define UTM_H #define UTM_H
// Choose floating point precision:
// 32-bit (for Teensy 3.5/3.6 ARM boards, etc.)
#define FLOAT_64
// 64-bit (for desktop/server use)
//#define FLOAT_64
#ifdef FLOAT_64
#define FLOAT double
#define SIN sin
#define COS cos
#define TAN tan
#define POW pow
#define SQRT sqrt
#define FLOOR floor
#else
#ifdef FLOAT_32
#define FLOAT float
#define SIN sinf
#define COS cosf
#define TAN tanf
#define POW powf
#define SQRT sqrtf
#define FLOOR floorf
#endif
#endif
#include <math.h>
#define pi 3.14159265358979
/* Ellipsoid model constants (actual values here are for WGS84) */
#define sm_a 6378137.0
#define sm_b 6356752.314
#define sm_EccSquared 6.69437999013e-03
#define UTMScaleFactor 0.9996
// DegToRad // DegToRad
// Converts degrees to radians. // Converts degrees to radians.
FLOAT DegToRad(FLOAT deg); double DegToRad(double deg);
// RadToDeg // RadToDeg
// Converts radians to degrees. // Converts radians to degrees.
FLOAT RadToDeg(FLOAT rad); double RadToDeg(double rad);
// ArcLengthOfMeridian // ArcLengthOfMeridian
// Computes the ellipsoidal distance from the equator to a point at a // Computes the ellipsoidal distance from the equator to a point at a
...@@ -84,7 +41,7 @@ FLOAT RadToDeg(FLOAT rad); ...@@ -84,7 +41,7 @@ FLOAT RadToDeg(FLOAT rad);
// //
// Returns: // Returns:
// The ellipsoidal distance of the point from the equator, in meters. // The ellipsoidal distance of the point from the equator, in meters.
FLOAT ArcLengthOfMeridian (FLOAT phi); double ArcLengthOfMeridian (double phi);
// UTMCentralMeridian // UTMCentralMeridian
// Determines the central meridian for the given UTM zone. // Determines the central meridian for the given UTM zone.
...@@ -95,7 +52,7 @@ FLOAT ArcLengthOfMeridian (FLOAT phi); ...@@ -95,7 +52,7 @@ FLOAT ArcLengthOfMeridian (FLOAT phi);
// Returns: // Returns:
// The central meridian for the given UTM zone, in radians // The central meridian for the given UTM zone, in radians
// Range of the central meridian is the radian equivalent of [-177,+177]. // Range of the central meridian is the radian equivalent of [-177,+177].
FLOAT UTMCentralMeridian(int zone); double UTMCentralMeridian(int zone);
// FootpointLatitude // FootpointLatitude
// //
...@@ -110,7 +67,7 @@ FLOAT UTMCentralMeridian(int zone); ...@@ -110,7 +67,7 @@ FLOAT UTMCentralMeridian(int zone);
// //
// Returns: // Returns:
// The footpoint latitude, in radians. // The footpoint latitude, in radians.
FLOAT FootpointLatitude(FLOAT y); double FootpointLatitude(double y);
// MapLatLonToXY // MapLatLonToXY
// Converts a latitude/longitude pair to x and y coordinates in the // Converts a latitude/longitude pair to x and y coordinates in the
...@@ -131,7 +88,7 @@ FLOAT FootpointLatitude(FLOAT y); ...@@ -131,7 +88,7 @@ FLOAT FootpointLatitude(FLOAT y);
// //
// Returns: // Returns:
// The function does not return a value. // The function does not return a value.
void MapLatLonToXY (FLOAT phi, FLOAT lambda, FLOAT lambda0, FLOAT &x, FLOAT &y); void MapLatLonToXY (double phi, double lambda, double lambda0, double &x, double &y);
// MapXYToLatLon // MapXYToLatLon
// Converts x and y coordinates in the Transverse Mercator projection to // Converts x and y coordinates in the Transverse Mercator projection to
...@@ -160,7 +117,7 @@ void MapLatLonToXY (FLOAT phi, FLOAT lambda, FLOAT lambda0, FLOAT &x, FLOAT &y); ...@@ -160,7 +117,7 @@ void MapLatLonToXY (FLOAT phi, FLOAT lambda, FLOAT lambda0, FLOAT &x, FLOAT &y);
// //
// x1frac, x2frac, x2poly, x3poly, etc. are to enhance readability and // x1frac, x2frac, x2poly, x3poly, etc. are to enhance readability and
// to optimize computations. // to optimize computations.
void MapXYToLatLon (FLOAT x, FLOAT y, FLOAT lambda0, FLOAT& phi, FLOAT& lambda); void MapXYToLatLon (double x, double y, double lambda0, double& phi, double& lambda);
// LatLonToUTMXY // LatLonToUTMXY
// Converts a latitude/longitude pair to x and y coordinates in the // Converts a latitude/longitude pair to x and y coordinates in the
...@@ -179,7 +136,7 @@ void MapXYToLatLon (FLOAT x, FLOAT y, FLOAT lambda0, FLOAT& phi, FLOAT& lambda); ...@@ -179,7 +136,7 @@ void MapXYToLatLon (FLOAT x, FLOAT y, FLOAT lambda0, FLOAT& phi, FLOAT& lambda);
// //
// Returns: // Returns:
// The UTM zone used for calculating the values of x and y. // The UTM zone used for calculating the values of x and y.
int LatLonToUTMXY (FLOAT lat, FLOAT lon, int zone, FLOAT& x, FLOAT& y); int LatLonToUTMXY (double lat, double lon, int zone, double& x, double& y);
// UTMXYToLatLon // UTMXYToLatLon
// //
...@@ -200,7 +157,7 @@ int LatLonToUTMXY (FLOAT lat, FLOAT lon, int zone, FLOAT& x, FLOAT& y); ...@@ -200,7 +157,7 @@ int LatLonToUTMXY (FLOAT lat, FLOAT lon, int zone, FLOAT& x, FLOAT& y);
// //
// Returns: // Returns:
// The function does not return a value. // The function does not return a value.
void UTMXYToLatLon (FLOAT x, FLOAT y, int zone, bool southhemi, FLOAT& lat, FLOAT& lon); void UTMXYToLatLon (double x, double y, int zone, bool southhemi, double& lat, double& lon);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment