limestone

A KDE marble commandline interface.
git clone git://r-36.net/limestone
Log | Files | Refs | README | LICENSE

limestone.cpp (4939B)


      1 /*
      2  * Copy me if you can.
      3  * by 20h
      4  */
      5 
      6 #include <QApplication>
      7 #include <QUrl>
      8 #include <QFileInfo>
      9 
     10 #include <marble/MarbleWidget.h>
     11 #include <marble/MarbleModel.h>
     12 
     13 #include <unistd.h>
     14 #include <stdio.h>
     15 #include <stdlib.h>
     16 #include <libgen.h>
     17 #include <locale.h>
     18 
     19 char *argv0;
     20 
     21 #include "arg.h"
     22 #include "config.h"
     23 
     24 using namespace Marble;
     25 
     26 int
     27 parseposition(char *s, float *lat, float *lon, float *alt)
     28 {
     29 	char *sb = NULL, *s1 = NULL, *s2 = NULL, *sd = NULL,
     30 	     *sd1 = NULL;
     31 	int isgood = 0;
     32 
     33 	sb = s;
     34 	for (; *sb == ' '; sb++);
     35 
     36 	sd = strstr(sb, "°");
     37 	s1 = strchr(s, ',');
     38 	if (sd > s1) {
     39 		*s1 = '.';
     40 		s1 = strchr(sb, ',');
     41 	}
     42 
     43 	if (s1 != NULL) {
     44 		*s1++ = '\0';
     45 		for (; *s1 == ' '; s1++);
     46 		s2 = strchr(s1, ',');
     47 		sd1 = strstr(s1, "°");
     48 	}
     49 
     50 	if (sd1 > s2) {
     51 		*s2 = '.';
     52 		s2 = strchr(s2, ',');
     53 	}
     54 
     55 	if (s2 != NULL) {
     56 		*s2++ = '\0';
     57 		for (; *s2 == ' '; s2++);
     58 	}
     59 
     60 	if (lat != NULL) {
     61 		*lat = strtof(sb, NULL);
     62 		isgood++;
     63 		if (sd != NULL) {
     64 			for (; *sd != 'W' && *sd != 'E' && *sd; sd++);
     65 			if (*sd == 'W')
     66 				*lat *= -1;
     67 		}
     68 	}
     69 	if (lon != NULL && s1 != NULL) {
     70 		*lon = strtof(s1, NULL);
     71 		isgood++;
     72 		if (sd1 != NULL) {
     73 			for (; *sd1 != 'S' && *sd1 != 'N' && *sd1; sd1++);
     74 			if (*sd1 == 'S')
     75 				*lon *= -1;
     76 		}
     77 	}
     78 	if (alt != NULL && s2 != NULL)
     79 		*alt = strtof(s2, NULL);
     80 
     81 	return (isgood > 1)? 0:1;
     82 }
     83 
     84 void
     85 applystylestring(MarbleWidget *mw, char *sstr)
     86 {
     87 	bool sv = false;
     88 	char *ss = NULL, *sp;
     89 
     90 	sp = ss = strdup(sstr);
     91 	if (ss == NULL)
     92 		return;
     93 
     94 	while (*ss) {
     95 		if (*ss >= 'A' && *ss <= 'Z') {
     96 			*ss = (*ss) - 'A' + 'a';
     97 			sv = true;
     98 		} else {
     99 			sv = false;
    100 		}
    101 
    102 		switch(*ss) {
    103 		case 'a':
    104 			mw->setShowAtmosphere(sv);
    105 			break;
    106 		case 'b':
    107 			mw->setShowBorders(sv);
    108 			break;
    109 		case 'c':
    110 			mw->setShowCompass(sv);
    111 			break;
    112 		case 'd':
    113 			mw->setShowClouds(sv);
    114 			break;
    115 		case 'e':
    116 			mw->setShowCities(sv);
    117 			break;
    118 		case 'f':
    119 			mw->setShowFrameRate(sv);
    120 			break;
    121 		case 'g':
    122 			mw->setShowGrid(sv);
    123 			break;
    124 		case 'h':
    125 			mw->setShowCrosshairs(sv);
    126 			break;
    127 		case 'i':
    128 			mw->setShowIceLayer(sv);
    129 			break;
    130 		case 'k':
    131 			mw->setShowBackground(sv);
    132 			break;
    133 		case 'l':
    134 			mw->setShowLakes(sv);
    135 			break;
    136 		case 'm':
    137 			mw->setShowOverviewMap(sv);
    138 			break;
    139 		case 'n':
    140 			mw->setShowTerrain(sv);
    141 			break;
    142 		case 'o':
    143 			mw->setShowOtherPlaces(sv);
    144 			break;
    145 		case 'p':
    146 			mw->setShowPlaces(sv);
    147 			break;
    148 		case 'r':
    149 			mw->setShowRelief(sv);
    150 			break;
    151 		case 's':
    152 			mw->setShowScaleBar(sv);
    153 			break;
    154 		case 't':
    155 			mw->setShowTileId(sv);
    156 			break;
    157 		case 'u':
    158 			mw->setShowRuntimeTrace(sv);
    159 			break;
    160 		case 'v':
    161 			mw->setShowRivers(sv);
    162 			break;
    163 		case 'w':
    164 			mw->setShowSunShading(sv);
    165 			break;
    166 		case 'y':
    167 			mw->setShowCityLights(sv);
    168 			break;
    169 		default:
    170 			continue;
    171 		};
    172 		ss++;
    173 	}
    174 	free(sp);
    175 }
    176 
    177 void
    178 usage(void)
    179 {
    180 	fprintf(stderr, "usage: %s [-h] "\
    181 			"[-k datafile] "\
    182 			"[-p lat,lon[,alt]] "\
    183 			"[-s stylestring] "\
    184 			"[-t theme] "\
    185 			"[-w winid] "\
    186 			"[-z zoomlevel] "\
    187 			"[URI]\n", basename(argv0));
    188 	exit(1);
    189 }
    190 
    191 int
    192 main(int argc, char *argv[])
    193 {
    194 	QApplication app(argc, argv);
    195 	MarbleWidget *mapwidget;
    196 	GeoDataCoordinates *gpos;
    197 	QFileInfo *qfilei;
    198 
    199 	char *kmlfile = NULL;
    200 	char *stylestring = NULL;
    201 	char *theme = "earth/googlemaps/googlemaps.dgml";
    202 
    203 	char *uri = NULL;
    204 	char *upath = NULL;
    205 	char *uscheme = NULL;
    206 	QUrl qurl;
    207 
    208 	float lat = 0.0, lon = 0.0, alt = 0.0;
    209 	int zoomlevel = 2200;
    210 	bool doposition = false;
    211 
    212 	char *winid = NULL;
    213 
    214 	setlocale(LC_NUMERIC, "C");
    215 
    216 	ARGBEGIN {
    217 	case 'h':
    218 	case 'v':
    219 		usage();
    220 	case 'k':
    221 		kmlfile = EARGF(usage());
    222 		break;
    223 	case 'p':
    224 		if (parseposition(EARGF(usage()), &lat, &lon, &alt))
    225 			usage();
    226 		doposition = true;
    227 		break;
    228 	case 's':
    229 		stylestring = EARGF(usage());
    230 		break;
    231 	case 't':
    232 		theme = EARGF(usage());
    233 		break;
    234 	case 'w':
    235 		winid = EARGF(usage());
    236 		break;
    237 	case 'z':
    238 		zoomlevel = atoi(EARGF(usage()));
    239 		break;
    240 	} ARGEND;
    241 
    242 	if (argc > 0)
    243 		uri = argv[0];
    244 
    245 	if (uri != NULL) {
    246 		qurl = QUrl(uri);
    247 		uscheme = qurl.scheme().toUtf8().data();
    248 		upath = qurl.path().toUtf8().data();
    249 
    250 		if (!strncmp(uri, "worldview:", 10)) {
    251 			if (parseposition(upath+1, &lat, &lon, &alt))
    252 				usage();
    253 			doposition = true;
    254 		} else if (!strncmp(uri, "geo:", 4)) {
    255 			if (parseposition(upath, &lat, &lon, &alt))
    256 				usage();
    257 			doposition = true;
    258 		} else {
    259 			fprintf(stderr, "URI scheme '%s' not supported.\n",
    260 					uscheme);
    261 		}
    262 	}
    263 
    264 	mapwidget = new MarbleWidget();
    265 	mapwidget->setMapThemeId(theme);
    266 
    267 	applystylestring(mapwidget, "ABCDfgHIKLmNoprStuVWY");
    268 	if (stylestring != NULL)
    269 		applystylestring(mapwidget, stylestring);
    270 
    271 	if (doposition) {
    272 		gpos = new GeoDataCoordinates(lon, lat, alt,
    273 				GeoDataCoordinates::Degree);
    274 		mapwidget->centerOn(*gpos);
    275 	}
    276 
    277 	mapwidget->setZoom(zoomlevel);
    278 	mapwidget->resize(400, 300);
    279 
    280 	if (kmlfile != NULL) {
    281 		qfilei = new QFileInfo(kmlfile);
    282 		mapwidget->model()->addGeoDataFile(
    283 				qfilei->absoluteFilePath());
    284 	}
    285 
    286 	mapwidget->show();
    287 
    288 	return app.exec();
    289 }
    290