Skip to content

Instantly share code, notes, and snippets.

@shuding
Created December 25, 2014 06:24
Show Gist options
  • Save shuding/0abf74254e1fe0f2527a to your computer and use it in GitHub Desktop.
Save shuding/0abf74254e1fe0f2527a to your computer and use it in GitHub Desktop.
Convert KML to SVG
/*
* Copyright (C) Ds [email protected]
* File Name : convert_kml_to_svg.cpp
* Creation Time : 2014/12/25 12:37:27
* Environment : OS X 10.8 & Vim 7.3
*/
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#define rep(i, n) for(int i = 1; i <= n; ++i)
#define sqr(x) ((x) * (x))
#define pb push_back
#define mp make_pair
#define sign(x) (x < 0 ? -1 : (x > 0))
#define debug(x) cout << #x << " = " << x << endl
using namespace std;
int svgNum = 0, pCnt;
char s[1000], c;
string fileName, cityName;
long double x[1000000], y[1000000], z[1000000];
int main(){
freopen("data.kml", "r", stdin);
while (cin.getline(s, 1000)) {
int l = strlen(s);
if (l > 30) {
s[29] = 0;
if (!strcmp(s, "<SimpleData name=\"councilnam\"")) {
fileName = string("");
for (int i = 30; i < l && s[i] != '<'; ++i) {
if (s[i] == ' ')
fileName += '_';
else
fileName += tolower(s[i]);
}
cityName = fileName;
fileName += ".svg";
}
}
else if (!strcmp(s, "<LinearRing>")) {
freopen(fileName.c_str(), "w", stdout);
long double maxX = -1000, minX = 1000, maxY = -1000, minY = 1000;
long double imgWidth, imgHeight, lWidth, lHeight;
c = getchar();
while(c != '>')
c = getchar();
pCnt = 0;
while(scanf("%Lf,%Lf,%Lf", &x[pCnt], &y[pCnt], &z[pCnt])) {
maxX = max(x[pCnt], maxX);
minX = min(x[pCnt], minX);
maxY = max(y[pCnt], maxY);
minY = min(y[pCnt], minY);
++pCnt;
}
lWidth = maxX - minX;
lHeight = maxY - minY;
imgWidth = lWidth * 100 + 4;
imgHeight = lHeight * 100 + 4;
printf("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
printf("<!-- Created with Inkscape (http://www.inkscape.org/) -->\n");
printf("<svg\n");
printf(" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n");
printf(" xmlns:cc=\"http://creativecommons.org/ns#\"\n");
printf(" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n");
printf(" xmlns:svg=\"http://www.w3.org/2000/svg\"\n");
printf(" xmlns=\"http://www.w3.org/2000/svg\"\n");
printf(" xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\"\n");
printf(" xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\"\n");
printf(" version=\"1.0\"\n");
printf(" width=\"%.0Lf\"\n", floor(imgWidth));
printf(" height=\"%.0Lf\"\n", floor(imgHeight));
printf(" sodipodi:docname=\"%s\">\n", fileName.c_str());
printf(" <g inkscape:groupmode=\"layer\" id=\"group\" inkscape:label=\"Layer\">\n");
printf(" <path id=\"%s\" d=\"M", cityName.c_str());
for (int i = 0; i < pCnt; ++i) {
printf("%.5Lf,%.5Lf ", (x[i] - minX) * 100 + 2, (maxY - y[i]) * 100 + 2);
}
printf(" \" style=\"fill:#eee;fill-opacity:1;stroke:#000;stroke-width:0.1;stroke-linecap:round;stroke-linejoin:bevel;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none\" inkscape:connector-curvature=\"0\" />\n</g>\n</svg>\n");
c = getchar();
while(c != '\n')
c = getchar();
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment