本文是針對GeoTools快速搭建做的一個基本的步驟,詳細情況還請查看官網。
1、準備工作:
1、Eclipse下載:http://www.eclipse.org/downloads/ 哪個版本都行,最好根據個人需要進行下載;
2、GeoTools API下載:有兩種方式,第一種方式是通過svn下載,第二種方式是直接下載開發包,在這裏我個人用的是第二種,所以我案例就用的第二種下載的離線開發包,這種是不是最新的,如果想用最新的開發包,請使用第一種方式,下載地址:http://sourceforge.net/projects/geotools/files/。
2、QuickStart搭建:
QuickStart搭建有幾種方式,這方式是根據Eclipse創建Java項目的方式,一種直接創建Java項目,另外一種是創建Maven項目,本文是創建Java項目,如果想有Maven項目的步驟,請評論,如果多的話,我酌情處理,畢竟我也剛剛學習,大家共同討論。
1、創建Java項目:
2、創建GeoToolsAPI文件夾存放API用
3、右擊GeoToolsAPI,點擊導入:
4、點擊Next,選擇API壓縮包
5、右擊項目,點擊屬性(property):
6、點擊java build path/libraries/add jars,選擇剛纔導進來的所有。jar文件
7、點擊order and import,select all
8、創建QuickStart類:
9、代碼如下:
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File file = JFileDataStoreChooser.showOpenFile("shp", null);
if (file == null) {
return;
}
FileDataStore store = FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource featureSource = store.getFeatureSource();
// Create a map content and add our shapefile to it
MapContent map = new MapContent();
map.setTitle("Quickstart");
Style style = SLD.createSimpleStyle(featureSource.getSchema());
Layer layer = new FeatureLayer(featureSource, style);
map.addLayer(layer);
// Now display the map
JMapFrame.showMap(map);
}
10、運行,選擇數據:注意數據路徑不能有中文,不許是經緯度的(可能,我測試時用非經緯度的失敗了)