Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
2,894 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
var GeoJSON = function( geojson, options ){ | ||
|
||
var _geometryToGoogleMaps = function( geojsonGeometry, opts, geojsonProperties ){ | ||
|
||
var googleObj; | ||
|
||
switch ( geojsonGeometry.type ){ | ||
case "Point": | ||
opts.position = new google.maps.LatLng(geojsonGeometry.coordinates[1], geojsonGeometry.coordinates[0]); | ||
googleObj = new google.maps.Marker(opts); | ||
if (geojsonProperties) { | ||
googleObj.set("geojsonProperties", geojsonProperties); | ||
} | ||
break; | ||
|
||
case "MultiPoint": | ||
googleObj = []; | ||
for (var i = 0; i < geojsonGeometry.coordinates.length; i++){ | ||
opts.position = new google.maps.LatLng(geojsonGeometry.coordinates[i][1], geojsonGeometry.coordinates[i][0]); | ||
googleObj.push(new google.maps.Marker(opts)); | ||
} | ||
if (geojsonProperties) { | ||
for (var k = 0; k < googleObj.length; k++){ | ||
googleObj[k].set("geojsonProperties", geojsonProperties); | ||
} | ||
} | ||
break; | ||
|
||
case "LineString": | ||
var path = []; | ||
for (var i = 0; i < geojsonGeometry.coordinates.length; i++){ | ||
var coord = geojsonGeometry.coordinates[i]; | ||
var ll = new google.maps.LatLng(coord[1], coord[0]); | ||
path.push(ll); | ||
} | ||
opts.path = path; | ||
googleObj = new google.maps.Polyline(opts); | ||
if (geojsonProperties) { | ||
googleObj.set("geojsonProperties", geojsonProperties); | ||
} | ||
break; | ||
|
||
case "MultiLineString": | ||
googleObj = []; | ||
for (var i = 0; i < geojsonGeometry.coordinates.length; i++){ | ||
var path = []; | ||
for (var j = 0; j < geojsonGeometry.coordinates[i].length; j++){ | ||
var coord = geojsonGeometry.coordinates[i][j]; | ||
var ll = new google.maps.LatLng(coord[1], coord[0]); | ||
path.push(ll); | ||
} | ||
opts.path = path; | ||
googleObj.push(new google.maps.Polyline(opts)); | ||
} | ||
if (geojsonProperties) { | ||
for (var k = 0; k < googleObj.length; k++){ | ||
googleObj[k].set("geojsonProperties", geojsonProperties); | ||
} | ||
} | ||
break; | ||
|
||
case "Polygon": | ||
var paths = []; | ||
var exteriorDirection; | ||
var interiorDirection; | ||
for (var i = 0; i < geojsonGeometry.coordinates.length; i++){ | ||
var path = []; | ||
for (var j = 0; j < geojsonGeometry.coordinates[i].length; j++){ | ||
var ll = new google.maps.LatLng(geojsonGeometry.coordinates[i][j][1], geojsonGeometry.coordinates[i][j][0]); | ||
path.push(ll); | ||
} | ||
if(!i){ | ||
exteriorDirection = _ccw(path); | ||
paths.push(path); | ||
}else if(i == 1){ | ||
interiorDirection = _ccw(path); | ||
if(exteriorDirection == interiorDirection){ | ||
paths.push(path.reverse()); | ||
}else{ | ||
paths.push(path); | ||
} | ||
}else{ | ||
if(exteriorDirection == interiorDirection){ | ||
paths.push(path.reverse()); | ||
}else{ | ||
paths.push(path); | ||
} | ||
} | ||
} | ||
opts.paths = paths; | ||
googleObj = new google.maps.Polygon(opts); | ||
if (geojsonProperties) { | ||
googleObj.set("geojsonProperties", geojsonProperties); | ||
} | ||
break; | ||
|
||
case "MultiPolygon": | ||
googleObj = []; | ||
for (var i = 0; i < geojsonGeometry.coordinates.length; i++){ | ||
var paths = []; | ||
var exteriorDirection; | ||
var interiorDirection; | ||
for (var j = 0; j < geojsonGeometry.coordinates[i].length; j++){ | ||
var path = []; | ||
for (var k = 0; k < geojsonGeometry.coordinates[i][j].length; k++){ | ||
var ll = new google.maps.LatLng(geojsonGeometry.coordinates[i][j][k][1], geojsonGeometry.coordinates[i][j][k][0]); | ||
path.push(ll); | ||
} | ||
if(!j){ | ||
exteriorDirection = _ccw(path); | ||
paths.push(path); | ||
}else if(j == 1){ | ||
interiorDirection = _ccw(path); | ||
if(exteriorDirection == interiorDirection){ | ||
paths.push(path.reverse()); | ||
}else{ | ||
paths.push(path); | ||
} | ||
}else{ | ||
if(exteriorDirection == interiorDirection){ | ||
paths.push(path.reverse()); | ||
}else{ | ||
paths.push(path); | ||
} | ||
} | ||
} | ||
opts.paths = paths; | ||
googleObj.push(new google.maps.Polygon(opts)); | ||
} | ||
if (geojsonProperties) { | ||
for (var k = 0; k < googleObj.length; k++){ | ||
googleObj[k].set("geojsonProperties", geojsonProperties); | ||
} | ||
} | ||
break; | ||
|
||
case "GeometryCollection": | ||
googleObj = []; | ||
if (!geojsonGeometry.geometries){ | ||
googleObj = _error("Invalid GeoJSON object: GeometryCollection object missing \"geometries\" member."); | ||
}else{ | ||
for (var i = 0; i < geojsonGeometry.geometries.length; i++){ | ||
googleObj.push(_geometryToGoogleMaps(geojsonGeometry.geometries[i], opts, geojsonProperties || null)); | ||
} | ||
} | ||
break; | ||
|
||
default: | ||
googleObj = _error("Invalid GeoJSON object: Geometry object must be one of \"Point\", \"LineString\", \"Polygon\" or \"MultiPolygon\"."); | ||
} | ||
|
||
return googleObj; | ||
|
||
}; | ||
|
||
var _error = function( message ){ | ||
|
||
return { | ||
type: "Error", | ||
message: message | ||
}; | ||
|
||
}; | ||
|
||
var _ccw = function( path ){ | ||
var isCCW; | ||
var a = 0; | ||
for (var i = 0; i < path.length-2; i++){ | ||
a += ((path[i+1].lat() - path[i].lat()) * (path[i+2].lng() - path[i].lng()) - (path[i+2].lat() - path[i].lat()) * (path[i+1].lng() - path[i].lng())); | ||
} | ||
if(a > 0){ | ||
isCCW = true; | ||
} | ||
else{ | ||
isCCW = false; | ||
} | ||
return isCCW; | ||
}; | ||
|
||
var obj; | ||
|
||
var opts = options || {}; | ||
|
||
switch ( geojson.type ){ | ||
|
||
case "FeatureCollection": | ||
if (!geojson.features){ | ||
obj = _error("Invalid GeoJSON object: FeatureCollection object missing \"features\" member."); | ||
}else{ | ||
obj = []; | ||
for (var i = 0; i < geojson.features.length; i++){ | ||
obj.push(_geometryToGoogleMaps(geojson.features[i].geometry, opts, geojson.features[i].properties)); | ||
} | ||
} | ||
break; | ||
|
||
case "GeometryCollection": | ||
if (!geojson.geometries){ | ||
obj = _error("Invalid GeoJSON object: GeometryCollection object missing \"geometries\" member."); | ||
}else{ | ||
obj = []; | ||
for (var i = 0; i < geojson.geometries.length; i++){ | ||
obj.push(_geometryToGoogleMaps(geojson.geometries[i], opts)); | ||
} | ||
} | ||
break; | ||
|
||
case "Feature": | ||
if (!( geojson.properties && geojson.geometry )){ | ||
obj = _error("Invalid GeoJSON object: Feature object missing \"properties\" or \"geometry\" member."); | ||
}else{ | ||
obj = _geometryToGoogleMaps(geojson.geometry, opts, geojson.properties); | ||
} | ||
break; | ||
|
||
case "Point": case "MultiPoint": case "LineString": case "MultiLineString": case "Polygon": case "MultiPolygon": | ||
obj = geojson.coordinates | ||
? obj = _geometryToGoogleMaps(geojson, opts) | ||
: _error("Invalid GeoJSON object: Geometry object missing \"coordinates\" member."); | ||
break; | ||
|
||
default: | ||
obj = _error("Invalid GeoJSON object: GeoJSON object must be one of \"Point\", \"LineString\", \"Polygon\", \"MultiPolygon\", \"Feature\", \"FeatureCollection\" or \"GeometryCollection\"."); | ||
|
||
} | ||
|
||
return obj; | ||
|
||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
# tokml.py: convert Kuntarajat2013.csv to single .kml and then convert it to | ||
# .geojson with GeoJSON.js. | ||
# | ||
# Author: Tomi.Mickelsson@iki.fi | ||
# 31.10.2013 - Created | ||
|
||
import sys | ||
import csv | ||
# import xml.etree.ElementTree as ET | ||
|
||
|
||
# increase limit | ||
csv.field_size_limit(500000) | ||
|
||
# inits lib | ||
def read(fname): | ||
# klist = [] | ||
# json = {"type":"FeatureCollection", "features":klist} | ||
|
||
dest = open("kunta.kml", "w") | ||
dest.write("""<?xml version="1.0" encoding="UTF-8"?> | ||
<kml xmlns="http://www.opengis.net/kml/2.2">""") | ||
|
||
with open(fname, 'rb') as csvfile: | ||
spamreader = csv.reader(csvfile, delimiter=',') | ||
for i, row in enumerate(spamreader): | ||
if i == 0: continue | ||
|
||
print(i, row[0]) | ||
dest.write(""" <Placemark> | ||
<name>%s</name>""" % row[0]) | ||
dest.write(row[2]) | ||
dest.write(""" </Placemark> | ||
""") | ||
|
||
# print(row[2]) | ||
|
||
# root = ET.fromstring(row[2]) | ||
# print root | ||
# for child in root.findall(": | ||
# print child.tag, child.attrib | ||
|
||
# if i > 50: break | ||
|
||
dest.write("</kml>") | ||
dest.close() | ||
|
||
# unit tests | ||
if __name__ == '__main__': | ||
read("Kuntarajat2013.csv") | ||
|
Oops, something went wrong.