source: subversion/applications/utils/srtm2shp/SRTMGeneral.cpp @ 34385

Last change on this file since 34385 was 6182, checked in by nick, 12 years ago

new and internationally-capable srtm2shp added

File size: 2.5 KB
Line 
1/*
2    Copyright (C) 2005 Nick Whitelegg, Hogweed Software, nick@hogweed.org
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU Lesser General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
17
18 */
19#include "SRTMGeneral.h"
20#include <fstream>
21#include <cmath>
22
23
24double min(double a,double b) { return (a<b) ? a:b; }
25double max(double a,double b) { return (a>b) ? a:b; }
26double between(double a, double b, double c) 
27        { return a>=min(b,c) && a<=max(b,c); }
28
29
30// Returns the slope angle of a contour line;
31// always in the range -90 -> 0 -> +90.
32// 08/02/05 made more generalised by passing parameters as x1,x2,y1,y2
33// rather than the line array.
34double slope_angle(double x1,double y1,double x2,double y2)
35{
36        double dy = y2-y1;
37        double dx = x2-x1;
38        double a = dx ? atan(dy/dx) : M_PI/2;
39
40        // minus to convert computer coord scheme (origin top left) to mathematical
41        // scheme (origin bottom left)
42        return -a; 
43}
44
45// reads a coast file from the rimmer.noaa site (?)
46
47vector<vector<EarthPoint> > readcoast (char* filename,
48                                EarthPoint bottomLeft, EarthPoint topRight) throw(string)
49{
50        char buf[1024];
51        std::ifstream infile(filename);
52        double lat, lon;
53        vector<vector<EarthPoint> > coastSegs;
54        vector<EarthPoint> curCoastSeg;
55
56
57        if(infile.good())
58        {
59                bool go = (bool)(infile.getline(buf,sizeof(buf))); // initial #
60       
61
62                while(go)
63                {
64                        curCoastSeg.clear();
65                        infile.getline(buf,sizeof(buf)); // first real line
66                        while(go && buf[0]!='#')
67                        {
68                                sscanf(buf,"%lf %lf", &lon, &lat);
69
70                                // Only include coordinates in the specified range
71                                if(lat>=bottomLeft.y&&lat<=topRight.y&&
72                                lon>=bottomLeft.x&&lon<=topRight.x)
73                                {
74                                        curCoastSeg.push_back(EarthPoint(lon,lat));
75                                }
76
77                                // Exit loop if this is a # or EOF
78                                go = (bool)(infile.getline(buf,sizeof(buf)));
79
80                        }
81
82                        coastSegs.push_back(curCoastSeg);
83                }
84        }
85        else
86        {
87                throw string("Error opening supplied file");
88        }
89        return coastSegs;
90}
91
92
Note: See TracBrowser for help on using the repository browser.