1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
/******************************************************************************
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: Generate an OGRSpatialReference object based on an EPSG
* PROJCS, or GEOGCS code.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2000, Frank Warmerdam
* Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_port.h"
#include "ogr_srs_api.h"
#include <cctype>
#include <cmath>
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <limits>
#include "cpl_conv.h"
#include "cpl_csv.h"
#include "cpl_error.h"
#include "cpl_multiproc.h"
#include "cpl_string.h"
#include "ogr_core.h"
#include "ogr_p.h"
#include "ogr_proj_p.h"
#include "ogr_spatialref.h"
#include "proj.h"
extern void OGRsnPrintDouble(char *pszStrBuf, size_t size, double dfValue);
/************************************************************************/
/* OSRGetEllipsoidInfo() */
/************************************************************************/
/**
* Fetch info about an ellipsoid.
*
* This helper function will return ellipsoid parameters corresponding to EPSG
* code provided. Axes are always returned in meters. Semi major computed
* based on inverse flattening where that is provided.
*
* @param nCode EPSG code of the requested ellipsoid
*
* @param ppszName pointer to string where ellipsoid name will be returned. It
* is caller responsibility to free this string after using with CPLFree().
*
* @param pdfSemiMajor pointer to variable where semi major axis will be
* returned.
*
* @param pdfInvFlattening pointer to variable where inverse flattening will
* be returned.
*
* @return OGRERR_NONE on success or an error code in case of failure.
**/
OGRErr OSRGetEllipsoidInfo(int nCode, char **ppszName, double *pdfSemiMajor,
double *pdfInvFlattening)
{
CPLString osCode;
osCode.Printf("%d", nCode);
auto ellipsoid = proj_create_from_database(
OSRGetProjTLSContext(), "EPSG", osCode.c_str(), PJ_CATEGORY_ELLIPSOID,
false, nullptr);
if (!ellipsoid)
{
return OGRERR_UNSUPPORTED_SRS;
}
if (ppszName)
{
*ppszName = CPLStrdup(proj_get_name(ellipsoid));
}
proj_ellipsoid_get_parameters(OSRGetProjTLSContext(), ellipsoid,
pdfSemiMajor, nullptr, nullptr,
pdfInvFlattening);
proj_destroy(ellipsoid);
return OGRERR_NONE;
}
/************************************************************************/
/* SetStatePlane() */
/************************************************************************/
/**
* \brief Set State Plane projection definition.
*
* This will attempt to generate a complete definition of a state plane
* zone based on generating the entire SRS from the EPSG tables. If the
* EPSG tables are unavailable, it will produce a stubbed LOCAL_CS definition
* and return OGRERR_FAILURE.
*
* This method is the same as the C function OSRSetStatePlaneWithUnits().
*
* @param nZone State plane zone number, in the USGS numbering scheme (as
* distinct from the Arc/Info and Erdas numbering scheme.
*
* @param bNAD83 TRUE if the NAD83 zone definition should be used or FALSE
* if the NAD27 zone definition should be used.
*
* @param pszOverrideUnitName Linear unit name to apply overriding the
* legal definition for this zone.
*
* @param dfOverrideUnit Linear unit conversion factor to apply overriding
* the legal definition for this zone.
*
* @return OGRERR_NONE on success, or OGRERR_FAILURE on failure, mostly likely
* due to the EPSG tables not being accessible.
*/
OGRErr OGRSpatialReference::SetStatePlane(int nZone, int bNAD83,
const char *pszOverrideUnitName,
double dfOverrideUnit)
{
/* -------------------------------------------------------------------- */
/* Get the index id from stateplane.csv. */
/* -------------------------------------------------------------------- */
if (!bNAD83 && nZone > INT_MAX - 10000)
return OGRERR_FAILURE;
const int nAdjustedId = bNAD83 ? nZone : nZone + 10000;
/* -------------------------------------------------------------------- */
/* Turn this into a PCS code. We assume there will only be one */
/* PCS corresponding to each Proj_ code since the proj code */
/* already effectively indicates NAD27 or NAD83. */
/* -------------------------------------------------------------------- */
char szID[32] = {};
snprintf(szID, sizeof(szID), "%d", nAdjustedId);
const int nPCSCode = atoi(CSVGetField(CSVFilename("stateplane.csv"), "ID",
szID, CC_Integer, "EPSG_PCS_CODE"));
if (nPCSCode < 1)
{
static bool bFailureReported = false;
if (!bFailureReported)
{
bFailureReported = true;
CPLError(CE_Warning, CPLE_OpenFailed,
"Unable to find state plane zone in stateplane.csv, "
"likely because the GDAL data files cannot be found. "
"Using incomplete definition of state plane zone.");
}
Clear();
if (bNAD83)
{
char szName[128] = {};
snprintf(szName, sizeof(szName), "State Plane Zone %d / NAD83",
nZone);
SetLocalCS(szName);
SetLinearUnits(SRS_UL_METER, 1.0);
}
else
{
char szName[128] = {};
snprintf(szName, sizeof(szName), "State Plane Zone %d / NAD27",
nZone);
SetLocalCS(szName);
SetLinearUnits(SRS_UL_US_FOOT, CPLAtof(SRS_UL_US_FOOT_CONV));
}
return OGRERR_FAILURE;
}
/* -------------------------------------------------------------------- */
/* Define based on a full EPSG definition of the zone. */
/* -------------------------------------------------------------------- */
OGRErr eErr = importFromEPSG(nPCSCode);
if (eErr != OGRERR_NONE)
return eErr;
/* -------------------------------------------------------------------- */
/* Apply units override if required. */
/* */
/* We will need to adjust the linear projection parameter to */
/* match the provided units, and clear the authority code. */
/* -------------------------------------------------------------------- */
if (pszOverrideUnitName != nullptr && dfOverrideUnit != 0.0 &&
fabs(dfOverrideUnit - GetLinearUnits()) > 0.0000000001)
{
const double dfFalseEasting = GetNormProjParm(SRS_PP_FALSE_EASTING);
const double dfFalseNorthing = GetNormProjParm(SRS_PP_FALSE_NORTHING);
SetLinearUnits(pszOverrideUnitName, dfOverrideUnit);
SetNormProjParm(SRS_PP_FALSE_EASTING, dfFalseEasting);
SetNormProjParm(SRS_PP_FALSE_NORTHING, dfFalseNorthing);
OGR_SRSNode *const poPROJCS = GetAttrNode("PROJCS");
if (poPROJCS != nullptr && poPROJCS->FindChild("AUTHORITY") != -1)
{
poPROJCS->DestroyChild(poPROJCS->FindChild("AUTHORITY"));
}
}
return OGRERR_NONE;
}
/************************************************************************/
/* OSRSetStatePlane() */
/************************************************************************/
/**
* \brief Set State Plane projection definition.
*
* This function is the same as OGRSpatialReference::SetStatePlane().
*/
OGRErr OSRSetStatePlane(OGRSpatialReferenceH hSRS, int nZone, int bNAD83)
{
VALIDATE_POINTER1(hSRS, "OSRSetStatePlane", OGRERR_FAILURE);
return reinterpret_cast<OGRSpatialReference *>(hSRS)->SetStatePlane(nZone,
bNAD83);
}
/************************************************************************/
/* OSRSetStatePlaneWithUnits() */
/************************************************************************/
/**
* \brief Set State Plane projection definition.
*
* This function is the same as OGRSpatialReference::SetStatePlane().
*/
OGRErr OSRSetStatePlaneWithUnits(OGRSpatialReferenceH hSRS, int nZone,
int bNAD83, const char *pszOverrideUnitName,
double dfOverrideUnit)
{
VALIDATE_POINTER1(hSRS, "OSRSetStatePlaneWithUnits", OGRERR_FAILURE);
return reinterpret_cast<OGRSpatialReference *>(hSRS)->SetStatePlane(
nZone, bNAD83, pszOverrideUnitName, dfOverrideUnit);
}
/************************************************************************/
/* AutoIdentifyEPSG() */
/************************************************************************/
/**
* \brief Set EPSG authority info if possible.
*
* This method inspects a WKT definition, and adds EPSG authority nodes
* where an aspect of the coordinate system can be easily and safely
* corresponded with an EPSG identifier. In practice, this method will
* evolve over time. In theory it can add authority nodes for any object
* (i.e. spheroid, datum, GEOGCS, units, and PROJCS) that could have an
* authority node. Mostly this is useful to inserting appropriate
* PROJCS codes for common formulations (like UTM n WGS84).
*
* If it success the OGRSpatialReference is updated in place, and the
* method return OGRERR_NONE. If the method fails to identify the
* general coordinate system OGRERR_UNSUPPORTED_SRS is returned but no
* error message is posted via CPLError().
*
* This method is the same as the C function OSRAutoIdentifyEPSG().
*
* Since GDAL 2.3, the FindMatches() method can also be used for improved
* matching by researching the EPSG catalog.
*
* @return OGRERR_NONE or OGRERR_UNSUPPORTED_SRS.
*
* @see OGRSpatialReference::FindBestMatch()
*/
OGRErr OGRSpatialReference::AutoIdentifyEPSG()
{
/* -------------------------------------------------------------------- */
/* Do we have a GEOGCS node, but no authority? If so, try */
/* guessing it. */
/* -------------------------------------------------------------------- */
if ((IsProjected() || IsGeographic()) &&
GetAuthorityCode("GEOGCS") == nullptr)
{
const int nGCS = GetEPSGGeogCS();
if (nGCS != -1)
SetAuthority("GEOGCS", "EPSG", nGCS);
}
if (IsProjected() && GetAuthorityCode("PROJCS") == nullptr)
{
const char *pszProjection = GetAttrValue("PROJECTION");
/* --------------------------------------------------------------------
*/
/* Is this a UTM coordinate system with a common GEOGCS? */
/* --------------------------------------------------------------------
*/
int nZone = 0;
int bNorth = FALSE;
if ((nZone = GetUTMZone(&bNorth)) != 0)
{
const char *pszAuthName = GetAuthorityName("PROJCS|GEOGCS");
const char *pszAuthCode = GetAuthorityCode("PROJCS|GEOGCS");
if (pszAuthName == nullptr || pszAuthCode == nullptr)
{
// Don't exactly recognise datum.
}
else if (EQUAL(pszAuthName, "EPSG") && atoi(pszAuthCode) == 4326)
{
// WGS84
if (bNorth)
SetAuthority("PROJCS", "EPSG", 32600 + nZone);
else
SetAuthority("PROJCS", "EPSG", 32700 + nZone);
}
else if (EQUAL(pszAuthName, "EPSG") && atoi(pszAuthCode) == 4267 &&
nZone >= 3 && nZone <= 22 && bNorth)
{
SetAuthority("PROJCS", "EPSG", 26700 + nZone); // NAD27
}
else if (EQUAL(pszAuthName, "EPSG") && atoi(pszAuthCode) == 4269 &&
nZone >= 3 && nZone <= 23 && bNorth)
{
SetAuthority("PROJCS", "EPSG", 26900 + nZone); // NAD83
}
else if (EQUAL(pszAuthName, "EPSG") && atoi(pszAuthCode) == 4322)
{ // WGS72
if (bNorth)
SetAuthority("PROJCS", "EPSG", 32200 + nZone);
else
SetAuthority("PROJCS", "EPSG", 32300 + nZone);
}
}
/* --------------------------------------------------------------------
*/
/* Is this a Polar Stereographic system on WGS 84 ? */
/* --------------------------------------------------------------------
*/
else if (pszProjection != nullptr &&
EQUAL(pszProjection, SRS_PT_POLAR_STEREOGRAPHIC))
{
const char *pszAuthName = GetAuthorityName("PROJCS|GEOGCS");
const char *pszAuthCode = GetAuthorityCode("PROJCS|GEOGCS");
const double dfLatOrigin =
GetNormProjParm(SRS_PP_LATITUDE_OF_ORIGIN, 0.0);
if (pszAuthName != nullptr && EQUAL(pszAuthName, "EPSG") &&
pszAuthCode != nullptr && atoi(pszAuthCode) == 4326 &&
fabs(fabs(dfLatOrigin) - 71.0) < 1e-15 &&
fabs(GetNormProjParm(SRS_PP_CENTRAL_MERIDIAN, 0.0)) < 1e-15 &&
fabs(GetProjParm(SRS_PP_SCALE_FACTOR, 1.0) - 1.0) < 1e-15 &&
fabs(GetNormProjParm(SRS_PP_FALSE_EASTING, 0.0)) < 1e-15 &&
fabs(GetNormProjParm(SRS_PP_FALSE_NORTHING, 0.0)) < 1e-15 &&
fabs(GetLinearUnits() - 1.0) < 1e-15)
{
if (dfLatOrigin > 0)
// Arctic Polar Stereographic
SetAuthority("PROJCS", "EPSG", 3995);
else
// Antarctic Polar Stereographic
SetAuthority("PROJCS", "EPSG", 3031);
}
}
}
/* -------------------------------------------------------------------- */
/* Return. */
/* -------------------------------------------------------------------- */
if (IsProjected() && GetAuthorityCode("PROJCS") != nullptr)
return OGRERR_NONE;
if (IsGeographic() && GetAuthorityCode("GEOGCS") != nullptr)
return OGRERR_NONE;
return OGRERR_UNSUPPORTED_SRS;
}
/************************************************************************/
/* OSRAutoIdentifyEPSG() */
/************************************************************************/
/**
* \brief Set EPSG authority info if possible.
*
* This function is the same as OGRSpatialReference::AutoIdentifyEPSG().
*
* Since GDAL 2.3, the OSRFindMatches() function can also be used for improved
* matching by researching the EPSG catalog.
*
*/
OGRErr OSRAutoIdentifyEPSG(OGRSpatialReferenceH hSRS)
{
VALIDATE_POINTER1(hSRS, "OSRAutoIdentifyEPSG", OGRERR_FAILURE);
return reinterpret_cast<OGRSpatialReference *>(hSRS)->AutoIdentifyEPSG();
}
|