整体更新

This commit is contained in:
hukekuan@163.com 2017-06-06 17:33:51 +08:00
parent d946d8ee15
commit bf41e4b225
2 changed files with 57 additions and 45 deletions

View File

@ -1,9 +1,11 @@
package com.gis3c; package com.gis3c;
import com.gis3c.entity.C3Map; import com.gis3c.entity.C3Map;
import org.apache.commons.dbcp.datasources.SharedPoolDataSource;
import org.geotools.data.FeatureSource; import org.geotools.data.FeatureSource;
import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder; import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.factory.CommonFactoryFinder; import org.geotools.factory.CommonFactoryFinder;
import org.geotools.map.FeatureLayer; import org.geotools.map.FeatureLayer;
@ -20,65 +22,68 @@ import org.opengis.feature.simple.SimpleFeatureType;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
public class Main { public class Main {
public static StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(); public static StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();
public static Style createStyle(File file, FeatureSource featureSource) { // public static Style createStyle(File file, FeatureSource featureSource) {
File sld = toSLDFile(file); // File sld = toSLDFile(file);
if (sld != null) { // if (sld != null) {
return createFromSLD(sld); // return createFromSLD(sld);
} // }
//
SimpleFeatureType schema = (SimpleFeatureType)featureSource.getSchema(); // SimpleFeatureType schema = (SimpleFeatureType)featureSource.getSchema();
return JSimpleStyleDialog.showDialog(null, schema); // return JSimpleStyleDialog.showDialog(null, schema);
} // }
public static File toSLDFile(File file) { // public static File toSLDFile(File file) {
String path = file.getAbsolutePath(); // String path = file.getAbsolutePath();
String base = path.substring(0,path.length()-4); // String base = path.substring(0,path.length()-4);
String newPath = base + ".sld"; // String newPath = base + ".sld";
File sld = new File( newPath ); // File sld = new File( newPath );
if( sld.exists() ){ // if( sld.exists() ){
return sld; // return sld;
} // }
newPath = base + ".SLD"; // newPath = base + ".SLD";
sld = new File( newPath ); // sld = new File( newPath );
if( sld.exists() ){ // if( sld.exists() ){
return sld; // return sld;
} // }
return null; // return null;
} // }
public static Style createFromSLD(File sld) { // public static Style createFromSLD(File sld) {
try { // try {
SLDParser stylereader = new SLDParser(styleFactory, sld.toURI().toURL()); // SLDParser stylereader = new SLDParser(styleFactory, sld.toURI().toURL());
Style[] style = stylereader.readXML(); // Style[] style = stylereader.readXML();
return style[0]; // return style[0];
//
} catch (Exception e) { // } catch (Exception e) {
JExceptionReporter.showDialog(e, "Problem creating style"); // JExceptionReporter.showDialog(e, "Problem creating style");
} // }
return null; // return null;
} // }
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
File file = JFileDataStoreChooser.showOpenFile("shp",null); File file = JFileDataStoreChooser.showOpenFile("shp",null);
if(file == null){ if(file == null){
return; return;
} }
File sldFile = new File("D:\\f\\shp\\shijie.sld"); 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); FileDataStore store = FileDataStoreFinder.getDataStore(file);
((ShapefileDataStore)store).setCharset(Charset.forName("GBK"));
SimpleFeatureSource featureSource = store.getFeatureSource(); SimpleFeatureSource featureSource = store.getFeatureSource();
C3Map map = new C3Map(); C3Map map = new C3Map();
map.setTitle("Quickstart"); map.setTitle("Quickstart");
// Style style = SLD.createSimpleStyle(featureSource.getSchema()); Style style = SLD.createSimpleStyle(featureSource.getSchema());
Style style = createStyle(file, featureSource);
Layer layer = new FeatureLayer(featureSource,style); Layer layer = new FeatureLayer(featureSource,style1);
map.addLayer(layer); map.addLayer(layer);
// map.saveImage("D://mapimage//1.jpg",256); map.saveImage("D://mapimage//1.jpg",256);
System.out.println("image has saved");
JMapFrame.showMap(map); // JMapFrame.showMap(map);
} }

View File

@ -5,6 +5,7 @@ import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.PNGEncodeParam; import com.sun.media.jai.codec.PNGEncodeParam;
import org.geotools.geometry.jts.ReferencedEnvelope; import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.map.MapContent; import org.geotools.map.MapContent;
import org.geotools.map.MapViewport;
import org.geotools.renderer.GTRenderer; import org.geotools.renderer.GTRenderer;
import org.geotools.renderer.lite.StreamingRenderer; 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 { public void saveImage(final String file,final int imageWidth) throws IOException {
GTRenderer renderer = new StreamingRenderer(); GTRenderer renderer = new StreamingRenderer();
renderer.setMapContent(this); renderer.setMapContent(this);
MapViewport mapViewport = this.getViewport();
ReferencedEnvelope mapBounds 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(); // = this.getMaxBounds();
mapViewport.setBounds(mapBounds);
double heightToWidth = mapBounds.getSpan(1)/mapBounds.getSpan(0); double heightToWidth = mapBounds.getSpan(1)/mapBounds.getSpan(0);
Rectangle imageBounds Rectangle imageBounds
// = new Rectangle(0,0,imageWidth, (int) Math.round(imageWidth*heightToWidth)); // = new Rectangle(0,0,imageWidth, (int) Math.round(imageWidth*heightToWidth));