diff --git a/src/main/java/com/gis3c/Main.java b/src/main/java/com/gis3c/Main.java index e886063..d5948d1 100644 --- a/src/main/java/com/gis3c/Main.java +++ b/src/main/java/com/gis3c/Main.java @@ -1,9 +1,11 @@ package com.gis3c; import com.gis3c.entity.C3Map; +import org.apache.commons.dbcp.datasources.SharedPoolDataSource; import org.geotools.data.FeatureSource; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; +import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.factory.CommonFactoryFinder; import org.geotools.map.FeatureLayer; @@ -20,65 +22,68 @@ import org.opengis.feature.simple.SimpleFeatureType; import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; public class Main { public static StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(); - public static Style createStyle(File file, FeatureSource featureSource) { - File sld = toSLDFile(file); - if (sld != null) { - return createFromSLD(sld); - } - - SimpleFeatureType schema = (SimpleFeatureType)featureSource.getSchema(); - return JSimpleStyleDialog.showDialog(null, schema); - } - public static File toSLDFile(File file) { - String path = file.getAbsolutePath(); - String base = path.substring(0,path.length()-4); - String newPath = base + ".sld"; - File sld = new File( newPath ); - if( sld.exists() ){ - return sld; - } - newPath = base + ".SLD"; - sld = new File( newPath ); - if( sld.exists() ){ - return sld; - } - return null; - } - public static Style createFromSLD(File sld) { - try { - SLDParser stylereader = new SLDParser(styleFactory, sld.toURI().toURL()); - Style[] style = stylereader.readXML(); - return style[0]; - - } catch (Exception e) { - JExceptionReporter.showDialog(e, "Problem creating style"); - } - return null; - } +// public static Style createStyle(File file, FeatureSource featureSource) { +// File sld = toSLDFile(file); +// if (sld != null) { +// return createFromSLD(sld); +// } +// +// SimpleFeatureType schema = (SimpleFeatureType)featureSource.getSchema(); +// return JSimpleStyleDialog.showDialog(null, schema); +// } +// public static File toSLDFile(File file) { +// String path = file.getAbsolutePath(); +// String base = path.substring(0,path.length()-4); +// String newPath = base + ".sld"; +// File sld = new File( newPath ); +// if( sld.exists() ){ +// return sld; +// } +// newPath = base + ".SLD"; +// sld = new File( newPath ); +// if( sld.exists() ){ +// return sld; +// } +// return null; +// } +// public static Style createFromSLD(File sld) { +// try { +// SLDParser stylereader = new SLDParser(styleFactory, sld.toURI().toURL()); +// Style[] style = stylereader.readXML(); +// return style[0]; +// +// } catch (Exception e) { +// JExceptionReporter.showDialog(e, "Problem creating style"); +// } +// return null; +// } public static void main(String[] args) throws IOException { File file = JFileDataStoreChooser.showOpenFile("shp",null); if(file == null){ return; } File sldFile = new File("D:\\f\\shp\\shijie.sld"); - + SLDParser styleReader = new SLDParser(styleFactory,sldFile.toURI().toURL()); + Style[] styles = styleReader.readXML(); + Style style1 = styles[0]; FileDataStore store = FileDataStoreFinder.getDataStore(file); + ((ShapefileDataStore)store).setCharset(Charset.forName("GBK")); SimpleFeatureSource featureSource = store.getFeatureSource(); C3Map map = new C3Map(); map.setTitle("Quickstart"); -// Style style = SLD.createSimpleStyle(featureSource.getSchema()); - Style style = createStyle(file, featureSource); + Style style = SLD.createSimpleStyle(featureSource.getSchema()); - Layer layer = new FeatureLayer(featureSource,style); + Layer layer = new FeatureLayer(featureSource,style1); map.addLayer(layer); -// map.saveImage("D://mapimage//1.jpg",256); - - JMapFrame.showMap(map); + map.saveImage("D://mapimage//1.jpg",256); + System.out.println("image has saved"); +// JMapFrame.showMap(map); } diff --git a/src/main/java/com/gis3c/entity/C3Map.java b/src/main/java/com/gis3c/entity/C3Map.java index f317536..68193cc 100644 --- a/src/main/java/com/gis3c/entity/C3Map.java +++ b/src/main/java/com/gis3c/entity/C3Map.java @@ -5,6 +5,7 @@ import com.sun.media.jai.codec.ImageEncoder; import com.sun.media.jai.codec.PNGEncodeParam; import org.geotools.geometry.jts.ReferencedEnvelope; import org.geotools.map.MapContent; +import org.geotools.map.MapViewport; import org.geotools.renderer.GTRenderer; import org.geotools.renderer.lite.StreamingRenderer; @@ -53,12 +54,18 @@ public class C3Map extends MapContent { public void saveImage(final String file,final int imageWidth) throws IOException { GTRenderer renderer = new StreamingRenderer(); renderer.setMapContent(this); - + MapViewport mapViewport = this.getViewport(); ReferencedEnvelope mapBounds - = new ReferencedEnvelope(117.45335889533,121.20518244635,35.753480445714,37.557981231236,this.getCoordinateReferenceSystem()); + = new ReferencedEnvelope( + 118.02532161784, + 118.08394386082, + 36.846653667618, + 36.818458342844, + this.getCoordinateReferenceSystem() + ); // = this.getMaxBounds(); - + mapViewport.setBounds(mapBounds); double heightToWidth = mapBounds.getSpan(1)/mapBounds.getSpan(0); Rectangle imageBounds // = new Rectangle(0,0,imageWidth, (int) Math.round(imageWidth*heightToWidth));