1 /******************************************************************************
2 * Copyright (c) 2020, Hobu Inc.
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following
8 * conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided
15 * with the distribution.
16 * * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
17 * names of its contributors may be used to endorse or promote
18 * products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
33 ****************************************************************************/
34
35 #include <string>
36
37 #include <ogr_srs_api.h>
38 #include <ogr_api.h>
39 #include <cpl_conv.h>
40
41 #include "SpatialRef.hpp"
42
43 namespace pdal
44 {
45 namespace gdal
46 {
47
SpatialRef()48 SpatialRef::SpatialRef()
49 {
50 newRef(OSRNewSpatialReference(""));
51 }
52
53
SpatialRef(const std::string & srs)54 SpatialRef::SpatialRef(const std::string& srs)
55 {
56 newRef(OSRNewSpatialReference(""));
57 if (OSRSetFromUserInput(get(), srs.data()) != OGRERR_NONE)
58 m_ref.reset();
59 }
60
61
setFromLayer(OGRLayerH layer)62 void SpatialRef::setFromLayer(OGRLayerH layer)
63 {
64 if (layer)
65 {
66 OGRSpatialReferenceH s = OGR_L_GetSpatialRef(layer);
67 if (s)
68 {
69 OGRSpatialReferenceH clone = OSRClone(s);
70 newRef(clone);
71 }
72 }
73 }
74
75
operator bool() const76 SpatialRef::operator bool () const
77 {
78 return m_ref.get() != NULL;
79 }
80
81
get() const82 OGRSpatialReferenceH SpatialRef::get() const
83 {
84 return m_ref.get();
85 }
86
87
wkt() const88 std::string SpatialRef::wkt() const
89 {
90 std::string output;
91
92 if (m_ref.get())
93 {
94 char *pszWKT = NULL;
95 OSRExportToWkt(m_ref.get(), &pszWKT);
96 bool valid = (bool)*pszWKT;
97 output = pszWKT;
98 CPLFree(pszWKT);
99 }
100 return output;
101 }
102
103
empty() const104 bool SpatialRef::empty() const
105 {
106 return wkt().empty();
107 }
108
newRef(void * v)109 void SpatialRef::newRef(void *v)
110 {
111 m_ref = RefPtr(v, [](void* t){ OSRDestroySpatialReference(t); } );
112 }
113
114 } // namespace gdal
115 } // namespace pdal
116