當前位置:編程學習大全網 - 源碼下載 - java geotools

java geotools

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 */ List ?list?=?new?ArrayList (); 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; } }

  • 上一篇:軸承源代碼
  • 下一篇:線索平臺源代碼
  • copyright 2024編程學習大全網