1 // terrasync.cxx -- "JIT" scenery fetcher
3 // Written by Curtis Olson, started November 2002.
5 // Copyright (C) 2002 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <stdlib.h> // atoi() atof() abs() system()
26 #include <simgear/compiler.h>
31 #include <plib/netSocket.h>
34 #include <simgear/bucket/newbucket.hxx>
40 static string server = "scenery.flightgear.org";
41 static string source_module = "Scenery-0.9.5";
42 static string source_base = server + (string)"::" + source_module;
43 static string dest_base = "/dest/scenery/dir";
47 static void usage( const string& prog ) {
48 cout << "Usage: " << prog
49 << " -p <port> [ -s <rsync_source> ] -d <rsync_dest>" << endl;
54 static void parse_message( const string &msg, int *lat, int *lon ) {
58 // find GGA string and advance to start of lat
59 string::size_type pos = text.find( "$GPGGA" );
60 string tmp = text.substr( pos + 7 );
61 pos = text.find( "," );
62 tmp = tmp.substr( pos + 1 );
63 // cout << "-> " << tmp << endl;
65 // find lat then advance to start of hemisphere
66 pos = tmp.find( "," );
67 string lats = tmp.substr( 0, pos );
68 dlat = atof( lats.c_str() ) / 100.0;
69 tmp = tmp.substr( pos + 1 );
71 // find N/S hemisphere and advance to start of lon
72 if ( tmp.substr( 0, 1 ) == "S" ) {
75 pos = tmp.find( "," );
76 tmp = tmp.substr( pos + 1 );
79 pos = tmp.find( "," );
80 string lons = tmp.substr( 0, pos );
81 dlon = atof( lons.c_str() ) / 100.0;
82 tmp = tmp.substr( pos + 1 );
84 // find E/W hemisphere and advance to start of lon
85 if ( tmp.substr( 0, 1 ) == "W" ) {
104 static void sync_area( int lat, int lon ) {
106 int baselat, baselon;
109 int base = (int)(lat / 10);
110 if ( lat == base * 10 ) {
113 baselat = (base - 1) * 10;
117 baselat = (int)(lat / 10) * 10;
121 int base = (int)(lon / 10);
122 if ( lon == base * 10 ) {
125 baselon = (base - 1) * 10;
129 baselon = (int)(lon / 10) * 10;
135 // make container directory
136 char container_dir[512];
137 snprintf( container_dir, 512, "%s/%c%03d%c%02d",
138 dest_base.c_str(), EW, abs(baselon), NS, abs(baselat) );
139 snprintf( command, 512, "mkdir -p %s", container_dir );
140 cout << command << endl;
144 snprintf( dir, 512, "%c%03d%c%02d/%c%03d%c%02d",
145 EW, abs(baselon), NS, abs(baselat),
146 EW, abs(lon), NS, abs(lat) );
148 snprintf( command, 512,
149 "rsync --verbose --archive --delete --perms --owner --group %s/%s/ %s/%s",
150 source_base.c_str(), dir, dest_base.c_str(), dir );
151 cout << command << endl;
157 static void sync_areas( int lat, int lon, int lat_dir, int lon_dir ) {
158 // do current 1x1 degree area first
159 sync_area( lat, lon );
161 if ( lat_dir == 0 && lon_dir == 0 ) {
162 // now do surrounding 8 1x1 degree areas.
163 for ( int i = lat - 1; i <= lat + 1; ++i ) {
164 for ( int j = lon - 1; j <= lon + 1; ++j ) {
165 if ( i != lat || j != lon ) {
171 if ( lat_dir != 0 ) {
172 sync_area( lat + lat_dir, lon );
173 sync_area( lat + lat_dir, lon - 1 );
174 sync_area( lat + lat_dir, lon + 1 );
176 if ( lon_dir != 0 ) {
177 sync_area( lat, lon + lon_dir );
178 sync_area( lat - 1, lon + lon_dir );
179 sync_area( lat + 1, lon + lon_dir );
185 int main( int argc, char **argv ) {
187 char host[256] = ""; // accept messages from anyone
192 if ( (string)argv[i] == "-p" ) {
194 port = atoi( argv[i] );
195 } else if ( (string)argv[i] == "-s" ) {
197 source_base = argv[i];
198 } else if ( (string)argv[i] == "-d" ) {
208 // Must call this before any other net stuff
209 netInit( &argc,argv );
213 if ( ! s.open( false ) ) { // open a UDP socket
214 printf("error opening socket\n");
218 s.setBlocking( false );
220 if ( s.bind( host, port ) == -1 ) {
221 printf("error binding to port %d\n", port);
229 int last_lat = -9999;
230 int last_lon = -9999;
231 bool recv_msg = false;
235 while ( (len = s.recv(msg, maxlen, 0)) >= 0 ) {
239 parse_message( msg, &lat, &lon );
240 cout << "pos = " << lat << "," << lon << endl;
244 if ( lat != last_lat || lon != last_lon ) {
245 int lat_dir, lon_dir, dist;
246 if ( last_lat == -9999 || last_lon == -9999 ) {
247 lat_dir = lon_dir = 0;
249 dist = lat - last_lat;
251 lat_dir = dist / abs(dist);
255 dist = lon - last_lon;
257 lon_dir = dist / abs(dist);
262 cout << "lat = " << lat << " lon = " << lon << endl;
263 cout << "lat_dir = " << lat_dir << " " << " lon_dir = " << lon_dir << endl;
264 sync_areas( lat, lon, lat_dir, lon_dir );