geotools学习1从postgressql取shp并且展示(java postgis)
国内geotools资料太少了,项目要用到,硬着头皮学啊。。。。
package org.geotools.demo;
import java.awt.Color;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import org.apache.log4j.Logger;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.data.postgis.PostgisDataStore;
import org.geotools.data.postgis.PostgisDataStoreFactory;
import org.geotools.feature.FeatureIterator;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import com.vividsolutions.jts.io.ParseException;
public class GetPostgisData {
private static final Logger LOGGER = Logger.getLogger(“org.geotools.postgis”);
static PostgisDataStore pgDatastore;
static PostgisDataStoreFactory factory=new PostgisDataStoreFactory();
static FeatureSource fsBC;
@SuppressWarnings( “unchecked” )
private static void ConnPostGis(String dbtype,String URL, int port,String database,
String user,String password) {
Map params = new HashMap();
params.put(“dbtype”,”postgis”);
params.put( “host” , URL);
params.put( “port” , new Integer(port));
params.put( “database” , database);
params.put( “user” , user);
params.put( “passwd” , password);
try {
pgDatastore = (PostgisDataStore) DataStoreFinder.getDataStore(params);
if (pgDatastore != null ) {
System.out.println( ” 系统连接到位于: ” + URL + ” 的空间数据库 ” + database + ” 成功! ” );
} else {
System.out.println( ” 系统连接到位于: ” + URL + ” 的空间数据库 ” + database + ” 失败!请检查相关参数 ” );
}
} catch (IOException e) {
e.printStackTrace();
System.out.println( ” 系统连接到位于: ” + URL + ” 的空间数据库 ” + database + ” 失败!请检查相关参数 ” );
}
}
public static void main(String[] args) throws IOException, ParseException {
ConnPostGis( “” , “localhost” , 5432 , “mygis” , “postgres” , “root” );
/**/ /* 读取指定类型名的地理特征 */
FeatureSource fsBC = pgDatastore.getFeatureSource(“tl_2010_36_elsd10”);
FeatureIterator inte = fsBC.getFeatures().features();
Style shpStyle = SLD.createPolygonStyle(Color.YELLOW, null, 0.0f);
// Set up a MapContext with the two layers
final MapContext map = new DefaultMapContext();
map.setTitle(“ImageLab”);
map.addLayer(fsBC, shpStyle);
JMapFrame frame = new JMapFrame(map);
frame.setSize(800, 600);
frame.enableStatusBar(true);
frame.enableTool(JMapFrame.Tool.ZOOM, JMapFrame.Tool.PAN, JMapFrame.Tool.RESET);
frame.enableToolBar(true);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu menu = new JMenu(“Raster”);
menuBar.add(menu);
frame.repaint();
frame.setVisible(true);
}
}
转载自:https://blog.csdn.net/sxausgyy/article/details/7948522