前言
在GIS開發中,數據的幾何有效性直接影響分析結果的準確性。無效的幾何(如自相交、空洞或坐標錯誤)可能導致空間計算失敗或輸出偏差。無論是Shapefile、GeoJSON還是數據庫中的空間數據,幾何質檢都是數據處理中不可忽視的關鍵步驟。
本篇教程在之前文章的基礎上講解如何將使用GeoTools
檢驗Shapefile
數據幾何圖形的有效性。
開發環境
本文使用如下開發環境,以供參考。
時間:2025年
GeoTools:34-SNAPSHOT
IDE:IDEA2025.1.2
JDK:17
1. 安裝依賴
在pom.xml
文件中添加gt-shapefile
和gt-swing
兩個依賴。其中gt-shapefile
用于shp
數據的轉換處理,gt-swing
屬于圖形用戶界面工具庫,用于地理空間數據的可視化、交互操作和地圖應用的快速開發。
<dependencies><dependency><groupId>org.geotools</groupId><artifactId>gt-shapefile</artifactId><version>${geotools.version}</version></dependency><dependency><groupId>org.geotools</groupId><artifactId>gt-swing</artifactId><version>${geotools.version}</version></dependency><dependency><groupId>org.geotools</groupId><artifactId>gt-epsg-hsql</artifactId><version>${geotools.version}</version></dependency>
</dependencies>
<repositories><repository><id>osgeo</id><name>OSGeo Release Repository</name><url>https://repo.osgeo.org/repository/release/</url><snapshots><enabled>false</enabled></snapshots><releases><enabled>true</enabled></releases></repository><repository><id>osgeo-snapshot</id><name>OSGeo Snapshot Repository</name><url>https://repo.osgeo.org/repository/snapshot/</url><snapshots><enabled>true</enabled></snapshots><releases><enabled>false</enabled></releases></repository>
</repositories>
2. 創建工具類
在開發工具中創建驗證Geometry
類ValidateGeometry
。
在main
方法中調用顯示地圖方法。
public class ValidateGeometry {// 定義Shp源數據private File sourceFile;// 定義要素源private SimpleFeatureSource featureSource;// 定義地圖組件private MapContent map;public static void main(String[] args) throws Exception {ValidateGeometry validateGeometry = new ValidateGeometry();// 打開地圖顯示Shp文件validateGeometry.displayShapefile();}
定義Shp
顯示方法,將其添加到地圖窗口。通過在JMapFrame
的工具欄上添加一個按鈕來用于檢要素幾何對象是否有效(例如多邊形邊界是否閉合)。
// 顯示Shp數據
private void displayShapefile() throws Exception{sourceFile = JFileDataStoreChooser.showOpenFile("shp",null);if(sourceFile==null){System.out.println("Shp 文件未知錯誤,請重新選擇!");return;}// 數據倉庫FileDataStore dataStore = FileDataStoreFinder.getDataStore(sourceFile);// 獲取要素數據源featureSource = dataStore.getFeatureSource();// 創建地圖map = new MapContent();Style style = SLD.createSimpleStyle(featureSource.getSchema());Layer layer = new FeatureLayer(featureSource,style);// 添加圖層map.layers().add(layer);// 創建工具條JMapFrame mapFrame = new JMapFrame(map);mapFrame.enableToolBar(true);mapFrame.enableStatusBar(true);JToolBar toolBar = mapFrame.getToolBar();toolBar.addSeparator();toolBar.add(new JButton(new ValidateGeometryAction()));// 設置地圖窗口大小mapFrame.setSize(800,600);mapFrame.setVisible(true);
}
在上面的代碼中使用JMapFrame
創建了一個工具條,使用JButton
創建了一個按鈕,并將其添加到工具欄中。這個按鈕的會觸發一個動作ValidateGeometryAction
,檢驗幾何對象的有效性。
3. 驗證 Geometry
通過定義一個內部嵌套類ValidateGeometryAction
完成幾何對象有效性的檢驗,大部分工作由父類中的輔助方法完成。
// 定義內部嵌套類
class ValidateGeometryAction extends SafeAction {ValidateGeometryAction() {super("ValidateGeometryAction");putValue(Action.SHORT_DESCRIPTION, "check each geometry");}public void action(ActionEvent e) throws Throwable{// 記錄無效 Geometry 數量int numInvalid = validateFeatureGeometry(null);String msg;if(numInvalid==0){msg = "All feature geometries are valid";}else {msg = "Invalid feature geometries: " + numInvalid;}JOptionPane.showMessageDialog(null,msg,"Geometry Results",JOptionPane.INFORMATION_MESSAGE);}
}
當點擊該檢驗按鈕時,對Shp
數據的幾何對象進行驗證,使用JOptionPane
彈出框顯示輸出信息。
定義上文中的validateFeatureGeometry
方法,用于統計錯誤幾何圖形的數量。此方法檢查與shapefile
中的每個要素關聯的幾何對象是否存在常見問題(例如沒有閉合邊界的多邊形)。
private int validateFeatureGeometry(ProgressListener progress) throws Exception{final SimpleFeatureCollection featureCollection = featureSource.getFeatures();// 創建一個 FeatureVisitor 來檢查每個 fatureclass ValidationVisitor implements FeatureVisitor {public int numInvalidGeometries = 0;public void visit(Feature f) {SimpleFeature feature = (SimpleFeature) f;Geometry geometry = (Geometry) feature.getDefaultGeometry();if(geometry != null && !geometry.isValid()){numInvalidGeometries++;System.out.println("Invalid geometry: " + feature.getID());}}}ValidationVisitor validationVisitor = new ValidationVisitor();// 將檢驗器和進度條傳遞給要素集合featureCollection.accepts(validationVisitor,progress);return validationVisitor.numInvalidGeometries;
};
OpenLayers示例數據下載,請回復關鍵字:ol數據
全國信息化工程師-GIS 應用水平考試資料,請回復關鍵字:GIS考試
【GIS之路】 已經接入了智能助手,歡迎關注,歡迎提問。
歡迎訪問我的博客網站-長談GIS:
http://shanhaitalk.com
都看到這了,不要忘記點贊、收藏 + 關注 哦 !
本號不定時更新有關 GIS開發 相關內容,歡迎關注 !