Shape to SVG Map converter
SVG Map Profileの最も基本的な部分(空間参照系メタデータの埋め込み)のサンプルプログラムを公開します。ShapeファイルからSVG Mapコンテンツを生成します。
GeoTools を使用していますので、少しの修正で他の形式からの変換もできるでしょう。
またまた つたないコードですがご勘弁を・・
LGPLライセンスに基づいて、ご使用ください。
(2007.5.9 CRSのURIを改訂)
We post the sample code of Shape to SVG Map Converter which used GeoTools. This code includes the most basic part of SVG Map Profile (Coordinate Reference System metadata). Please use it based on the LGPL license.
// =============================================================================
// The basic SVG Map exporter program
//
// Copyright (C) 2007 by Satoru Takagi All rights reserved.
// http://blog.svg-map.com/
//
// Usage: java Shape2SvgMapBasic shapefile.shp
//
// This program uses geoTools2.3.0
// =============================================================================
//
// This software is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// =============================================================================
import java.io.*;
import java.io.IOException;
import java.net.URL;
import org.geotools.data.*;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.*;
import com.vividsolutions.jts.geom.*;
public class Shape2SvgMapBasic {
double boundHeight = 400.0; // SVG Height
double projCenter;
String fillColor = "green";
String strokeColor = "black";
double strokeWidth = 0.01;
// CRS:84 (WGS84 Long-Lat) This URI(URN) is ambiguous...
String CRS = "http://purl.org/crs/84";
double[] affineParam = new double[6];
public static void main(String[] args) {
Shape2SvgMapBasic s2sm = new Shape2SvgMapBasic();
try {
// Prepare an output SVG Map file
FileOutputStream osFile = new FileOutputStream( args[0] + ".svg" );
Writer fos = new BufferedWriter(new OutputStreamWriter( osFile , "UTF-8" ));
// Shape file
URL shapeURL = (new File(args[0])).toURL();
s2sm.parse( shapeURL , fos );
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void parse( URL shapeURL , Writer out ) throws Exception {
double maxX, minX, maxY, minY;
// Initialize shape file reader
ShapefileDataStore readStore = new ShapefileDataStore(shapeURL);
FeatureSource source = readStore.getFeatureSource();
// get envelope
Envelope env = source.getBounds();
maxX = env.getMaxX();
minX = env.getMinX();
maxY = env.getMaxY();
minY = env.getMinY();
// Make affine parameters for CRS metadata of SVG Map
// Equirectangular Projection (Standard parallel = Center of map)
//
// SVG_X = a * GEO_X + c * GEO_Y + e
// SVG_Y = b * GEO_X + d * GEO_Y + f
affineParam[1] = 0.0; // b
affineParam[2] = 0.0; // c
affineParam[3] = - boundHeight / ( maxY - minY ); // d
projCenter = ( maxY + minY ) / 2.0;
affineParam[0] = - affineParam[3] * Math.cos( projCenter * Math.PI / 180.0 ); // a
affineParam[4] = -affineParam[0] * minX; // e
affineParam[5] = -affineParam[3] * maxY; // f
// Make a SVG ViewBox
double origX , origY , cWidth , cHeight;
cWidth = Math.abs( ( maxX - minX ) * affineParam[0] );
cHeight = Math.abs( ( maxY - minY ) * affineParam[3] );
origX = affineParam[0] * minX + affineParam[4];
origY = affineParam[3] * maxY + affineParam[5];
// get FeatureCollection
FeatureCollection fsShape = source.getFeatures();
// Start SVG Map output
// SVG header etc.
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
out.write("<svg xmlns=\"http://www.w3.org/2000/svg\" ");
out.write("xmlns:xlink=\"http://www.w3.org/1999/xlink\" ");
out.write("viewBox=\"" + origX + " " + origY + " " + cWidth + " " + cHeight + "\" >\n");
// metadata for SVG Map
out.write("<metadata>\n");
// CRS metadata
out.write(" <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" ");
out.write("xmlns:crs=\"http://opengis.org/xmldtds/transformations.dtd\" xmlns:svg=\"http://www.w3.org/2000/svg\">\n");
out.write(" <rdf:Description>\n");
out.write(" <crs:CoordinateReferenceSystem rdf:resource=\"" + CRS + "\" ");
out.write("svg:transform=\"matrix(" + affineParam[0] + "," + affineParam[1] + "," + affineParam[2]
+ "," + affineParam[3] + "," + affineParam[4] + "," + affineParam[5] +")\" />\n");
out.write(" </rdf:Description>\n");
out.write(" </rdf:RDF>\n");
out.write("</metadata>\n");
// Icon for Point
out.write("<defs>\n");
out.write(" <g id=\"p0\" >\n");
out.write("<circle cx=\"0.0\" cy=\"0.0\" r=\"" + (cWidth / 300) + "\" stroke=\"none\" />\n");
out.write(" </g>\n");
out.write("</defs>\n");
// Main data body
FeatureIterator reader = fsShape.features();
Feature oneFeature;
Geometry oneGeom;
Coordinate[] coord;
while (reader.hasNext()) {
oneFeature = reader.next();
oneGeom = oneFeature.getDefaultGeometry();
parseGeometry(oneGeom , out );
}
reader.close();
out.write("</svg>\n");
}
private void parseGeometry(Geometry geom , Writer out ) throws Exception {
Coordinate[] coord;
Coordinate oneCrd = new Coordinate();
if (geom instanceof Polygon ){
out.write("<path ");
out.write("fill=\"" + fillColor + "\" fill-rule=\"evenodd\" d=\"M");
coord = (((Polygon)geom).getExteriorRing()).getCoordinates();
for ( int i = 0 ; i < coord.length ; i++ ){
if ( i!= 0 ){
out.write( "L" );
}
oneCrd = transform2D(coord[i] );
out.write( oneCrd.x + "," + oneCrd.y + " " );
}
out.write ("Z ");
for ( int j = 0 ; j < ((Polygon)geom).getNumInteriorRing() ; j++ ){
coord = (((Polygon)geom).getInteriorRingN(j)).getCoordinates();
for ( int i = 0 ; i < coord.length ; i++ ){
if ( i!= 0 ){
out.write( "L" );
} else {
out.write( "M" );
}
oneCrd = transform2D(coord[i] );
out.write( oneCrd.x + "," + oneCrd.y + " " );
}
out.write ("Z ");
}
out.write ("\"/>\n");
} else if (geom instanceof LineString ){
out.write("<polyline ");
out.write("fill=\"none\" stroke-width=\"" + strokeWidth + "\" stroke=\"" + strokeColor + "\" points=\"");
coord = ((LineString)geom).getCoordinates();
for ( int i = 0 ; i < coord.length ; i++ ){
oneCrd = transform2D(coord[i] );
out.write( oneCrd.x + "," + oneCrd.y + " " );
}
out.write ("\"/>\n");
} else if (geom instanceof Point ){
oneCrd = transform2D(((Point)geom).getCoordinate() );
out.write("<use ");
out.write("xlink:href=\"#p0\" fill=\"" + fillColor + "\"");
out.write(" x=\"" + oneCrd.x + "\"" + " y=\"" + oneCrd.y + "\"/>\n");
} else if (geom instanceof GeometryCollection ){
out.write("<g>\n");
for ( int j = 0 ; j < ((GeometryCollection)geom).getNumGeometries() ; j++){
Geometry childGeom = ((GeometryCollection)geom).getGeometryN(j);
parseGeometry(childGeom , out);
}
out.write("</g>\n");
} else if (geom instanceof Geometry ){
out.write("<!-- Type: Other Geometry...." + geom + "-->\n");
} else if (geom instanceof Object){
out.write("<!-- Type: Other Object...." + geom + "-->\n");
}
}
private Coordinate transform2D( Coordinate inCrd ){
Coordinate outCrd = new Coordinate(
affineParam[0] * inCrd.x + affineParam[2] * inCrd.y + affineParam[4] ,
affineParam[1] * inCrd.x + affineParam[3] * inCrd.y + affineParam[5] );
return (outCrd);
}
}