<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>
java geotools是什麼,讓我們一起了解一下?
Geotools是一個java類庫,提供了很多的標準類和方法來處理空間數據,同時這個類庫是構建在OGC標準之上的,是OGC思想的一種實現。使用Java語言和麪向對象方法時,按照功能劃分模組,結構清晰。
它的核心特點是什麼?
1、爲空間概念和數據結構定義了很多的接口。
2、透過JTS類庫集成了對幾何拓撲的支援。
3、透過使用OGC過濾編碼規範可以對屬性和空間要素過濾。
4、對於數據訪問API,支援要素訪問、事務支援和線程間鎖定。
5、可以訪問多種格式的數據和空間數據庫。
6、支援多種座標參考系統和及其轉換。
7、可以和擴展的地圖投影一同工作。
8、可以按照空間和非空間屬性來過濾和分析數據。
9、一種無狀態的,耗低內存的渲染機制,尤其在服務端環境下。
10、透過複雜的樣式(SLD)來組成和展現地圖。
實戰操作:
java如何用geotools類庫讀取shapefile?
shapefile是esri公司最先搞出來的,那麼arcgis應該是有相關的類庫的吧?好像找不到?我問過搞移動端的同事,arcgis for android確有處理shapefile的類庫,處理起來易如反掌。
但是,在WEB系統,服務器端從shapefile讀出數據,最終是要在前端瀏覽器中展示,像我們目前在建的項目,就是要用arcgis for js來展示這些數據,而安卓系統類似CS項目,有很大的不同。最大的不同,WEB系統中,數據要以JSON的形式給前端,這樣纔好處理。
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;import org.geotools.data.FileDataStore;import org.geotools.data.FileDataStoreFinder;import org.geotools.data.shapefile.ShapefileDataStore;import org.geotools.data.shapefile.dbf.DbaseFileHeader;import org.geotools.data.shapefile.dbf.DbaseFileReader;import org.geotools.data.shapefile.files.ShpFiles;import org.geotools.data.simple.SimpleFeatureCollection;import org.geotools.data.simple.SimpleFeatureIterator;import org.geotools.data.simple.SimpleFeatureSource;import org.geotools.geojson.feature.FeatureJSON;import org.geotools.geometry.jts.ReferencedEnvelope;import org.locationtech.jts.geom.Coordinate;import org.locationtech.jts.geom.Geometry;import org.opengis.feature.Property;import org.opengis.feature.simple.SimpleFeature;import org.opengis.feature.simple.SimpleFeatureType;import org.opengis.referencing.FactoryException;import org.opengis.referencing.crs.CoordinateReferenceSystem;import org.opengis.referencing.operation.TransformException;import java.io.*;import java.nio.charset.Charset;import java.util.*;/* shapefile操作類 */public class ShapefileHelper { public static Object read(String path) throws IOException { /*參數path就是shp檔案的完整路徑,如:E:蟠桃會資源清查調查圖斑.shp 系統會自動檢查同一個目錄下有沒有其他相關檔案,有的話會一併讀出,相關檔案的路徑無須給出.shp 存儲地理形狀和位置資訊.dbf 存儲屬性資訊.shx 索引檔案.prj 座標系.cpg 字元編碼,如UTF-8讀取出來的結果類型爲 List<Map>*/ List<Map> list = new ArrayList<Map>(); File file = getFile(path); if (file == null) { return list; } String charset = getCharSet(path); FileDataStore store = FileDataStoreFinder.getDataStore(file); ((ShapefileDataStore)store).setCharset(Charset.forName(charset)); SimpleFeatureSource featureSource = store.getFeatureSource(); SimpleFeatureCollection collection = featureSource.getFeatures(); SimpleFeatureIterator features = collection.features(); while (features.hasNext()) { Map item = new HashMap(); SimpleFeature f = features.next(); Collection p = f.getProperties(); Iterator it = p.iterator(); while (it.hasNext()) { Property pro = it.next(); String field = pro.getName().toString(); field = field.equals("the_geom") ? "wkt" : field; String value = pro.getValue().toString(); item.put(field, value); } list.add(item); } return list; } private static File getFile(String path){ File file = new File(path); if (file == null) { System.out.println("找不到路徑:" + path); } return file; } /* 獲取shapefile字元編碼 如果存在.cpg檔案,則從中讀取,否則默認爲UTF-8 */ private static String getCharSet(String path){ String charset = "UTF-8"; int p = path.lastIndexOf("."); String cpg = path.substring(0,p) + ".cpg"; File file = getFile(cpg); if(file != null) { RandomAccessFile raf = null; try { raf = new RandomAccessFile(cpg, "r"); charset = raf.readLine(); raf.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return charset; }}