Debian Codegraph @

src/algorithms/Egcs.cc

raw symbols SCIP
1 /**********************************************************************
2 Egcs.cc: Conversions from and to european grid coding system
3 Author(s): [email protected]
4 Copyright (C) 2016-2020 European Union (Joint Research Centre)
5
6 This file is part of jiplib.
7
8 jiplib is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 jiplib is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with jiplib. If not, see <https://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 #include "Egcs.h"
22 #include <iostream>
23 #include <sstream>
24 #include <iomanip>
25 #include <assert.h>
26
27 Egcs::Egcs(){
28 }
29
30 // Egcs::Egcs(unsigned short level)
31 // : m_level(level){
32 // }
33
34
35 Egcs::Egcs(unsigned short level)
36 {
37 m_level=level;
38 }
39
40 unsigned short Egcs::cell2level(const std::string& cellCode) const{
41 size_t pos=cellCode.find("-");
42 std::string TILE=cellCode.substr(0,pos);
43 unsigned short level=0;
44 int base_level=19-(TILE.size()/2-1)*3;
45 int quad_level=0;
46 if(pos!=std::string::npos)
47 quad_level=cellCode.size()-pos-1;
48 if(quad_level>1)
49 level=base_level-2;
50 else if(quad_level>0)
51 level=base_level-1;
52 else
53 level=base_level;
54 return level;
55 }
56
57 Egcs::~Egcs(){
58 }
59
60 unsigned short Egcs::res2level(double resolution) const{
61 double base=pow(10,log(resolution*4.0)/log(10.0));
62 double diff=base/(2*resolution);
63 return 0.5+(log(base)/log(10.0)*3+1-diff);
64 }
65
66 double Egcs::getResolution() const{
67 unsigned short exponent=(m_level+1)/3;
68 double base=pow(10.0,exponent);
69 if((m_level)%3==2)
70 return(base/4);
71 else if((m_level)%3==0)
72 return(base/2);
73 else
74 return(base);
75 }
76
77 void Egcs::force2grid(double& ulx, double& uly, double& lrx, double &lry) const{
78 double dx=getResolution();
79 double dy=dx;
80 //ulx
81 ulx=floor(ulx);
82 ulx-=static_cast<int>(ulx)%(static_cast<int>(dx));
83 //uly
84 uly=ceil(uly);
85 if(static_cast<int>(uly)%static_cast<int>(dy))
86 uly+=dy;
87 uly-=static_cast<int>(uly)%(static_cast<int>(dy));
88 //lrx
89 lrx=ceil(lrx);
90 if(static_cast<int>(lrx)%static_cast<int>(dx))
91 lrx+=dx;
92 lrx-=static_cast<int>(lrx)%(static_cast<int>(dx));
93 //lry
94 lry=floor(lry);
95 lry-=static_cast<int>(lry)%(static_cast<int>(dy));
96 }
97
98 void Egcs::cell2bb(const std::string& cellCode, int &ulx, int &uly, int &lrx, int &lry) const{
99 size_t pos=cellCode.find("-");
100 std::string TILE=cellCode.substr(0,pos);
101 std::string TILEX=TILE.substr(0,TILE.size()/2);
102 std::string TILEY=TILE.substr(TILE.size()/2);
103 std::string QUAD;
104 char QUAD1,QUAD2;
105 std::istringstream stilex(TILEX);
106 std::istringstream stiley(TILEY);
107 int llx,lly;
108 stilex >> llx;
109 stiley >> lly;
110 llx*=getBaseSize();
111 lly*=getBaseSize();
112 switch((19-m_level)%3){
113 case(0)://there should be no QUAD
114 assert(pos==std::string::npos);
115 break;
116 case(2)://there is a QUAD2
117 assert(pos+1!=std::string::npos);
118 QUAD=cellCode.substr(pos+1);
119 QUAD2=QUAD.substr(1,1).c_str()[0];
120 switch(QUAD2){
121 case('A'):
122 break;
123 case('C'):
124 lly+=getBaseSize()/4;
125 break;
126 case('D'):
127 lly+=getBaseSize()/4;
128 case('B'):
129 llx+=getBaseSize()/4;
130 break;
131 }
132 case(1)://QUAD1: deliberate fall through from case(2)!
133 if(!QUAD.size()){
134 assert(pos+1!=std::string::npos);
135 QUAD=cellCode.substr(pos+1);
136 }
137 QUAD1=QUAD.substr(0,1).c_str()[0];
138 switch(QUAD1){
139 case('A'):
140 break;
141 case('C'):
142 lly+=getBaseSize()/2;
143 break;
144 case('D'):
145 lly+=getBaseSize()/2;
146 case('B'):
147 llx+=getBaseSize()/2;
148 break;
149 }
150 break;
151 }
152 ulx=llx;
153 uly=static_cast<int>(lly+getSize());
154 lrx=static_cast<int>(llx+getSize());
155 lry=lly;
156 }
157
158 void Egcs::cell2mid(const std::string& cellCode, double& midX, double& midY) const{
159 int ulx,uly,lrx,lry;
160 cell2bb(cellCode,ulx,uly,lrx,lry);
161 midX=(ulx+lrx)/2.0;
162 midY=(lry+uly)/2.0;
163 }
164
165 std::string Egcs::geo2cell(double geoX, double geoY) const{
166 int ndgts=7-(m_level+1)/3;
167 double xcel=static_cast<int>(geoX)/getBaseSize();
168 double ycel=static_cast<int>(geoY)/getBaseSize();
169 std::ostringstream osx;
170 std::ostringstream osy, osxy;
171 // osx << setprecision(ndgts) << xcel;
172 // osy << setprecision(ndgts) << ycel;
173 // osx << setprecision(0) << fixed << geoX;
174 // osy << setprecision(0) << fixed << geoY;
175 osx << std::fixed << geoX;
176 osy << std::fixed << geoY;
177 std::string quad1="";
178 std::string quad2="";
179 switch((19-m_level)%3){
180 case(2):
181 if(static_cast<int>(geoX)%(getBaseSize()/2)>=getBaseSize()/2.0){
182 if(static_cast<int>(geoY)%(getBaseSize()/2)>=getBaseSize()/2.0)
183 quad2="D";
184 else
185 quad2="B";
186 }
187 else if(static_cast<int>(geoY)%getBaseSize()>=getBaseSize()/4.0)
188 quad2="C";
189 else
190 quad2="A";
191 case(1)://deliberate fall through!
192 if(static_cast<int>(geoX)%getBaseSize()>=getBaseSize()/2.0){
193 if(static_cast<int>(geoY)%getBaseSize()>=getBaseSize()/2.0)
194 quad1="-D";
195 else
196 quad1="-B";
197 }
198 else if(static_cast<int>(geoY)%getBaseSize()>=getBaseSize()/2.0)
199 quad1="-C";
200 else
201 quad1="-A";
202 break;
203 default:
204 break;
205 }
206 osxy << osx.str().substr(0,ndgts) << osy.str().substr(0,ndgts) << quad1 << quad2;
207 return osxy.str();
208 }
209