日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python如何使用geotools_基于GeoTools实现道路结点的提取

發(fā)布時(shí)間:2023/12/4 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python如何使用geotools_基于GeoTools实现道路结点的提取 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近公司的地圖業(yè)務(wù)數(shù)據(jù)換了供應(yīng)商,由于在進(jìn)行路徑規(guī)劃的時(shí)候需 要使用到道路結(jié)點(diǎn)進(jìn)行圖的構(gòu)建,因而需要根據(jù)道路圖層提取出道路的節(jié)點(diǎn)。因?yàn)榻?jīng)常使用arcpy,所以先用python寫了個(gè)版本,通常將數(shù)據(jù)放在地理數(shù)據(jù)庫(kù)中會(huì)有更高的運(yùn)行效率,這里是之前的代碼,并未存到文件地理數(shù)據(jù)庫(kù)中運(yùn)行,代碼如下:

import arcpy

import time,os

import math

print '程序開始: ' + str(time.ctime())

#設(shè)置工作環(huán)境 地理數(shù)據(jù)庫(kù)

from arcpy import env

env.workspace = r'E:\data\import'

#新建要素輸出路徑 地理數(shù)據(jù)庫(kù)

out_path = r'E:\data\import'

#輸入要素 地理數(shù)據(jù)庫(kù)中的文件

input_name = 'Road.shp'

start_name = 'Starts.shp'

end_name = 'Ends.shp'

node_name = 'Node.shp'

field_name = 'NodeID'

try:

arcpy.FeatureVerticesToPoints_management(input_name,start_name,"START")

print arcpy.GetMessages()

arcpy.AddField_management(start_name, field_name, "LONG", "", "")

print arcpy.GetMessages()

arcpy.CalculateField_management(start_name, field_name, "!SNodeID!","PYTHON_9.3")

print arcpy.GetMessages()

arcpy.FeatureVerticesToPoints_management(input_name,end_name,"END")

print arcpy.GetMessages()

arcpy.AddField_management(end_name, field_name, "LONG", "", "")

print arcpy.GetMessages()

arcpy.CalculateField_management(end_name, field_name, "!ENodeID!","PYTHON_9.3")

print arcpy.GetMessages()

arcpy.Merge_management([start_name, end_name], node_name)

print arcpy.GetMessages()

arcpy.DeleteIdentical_management(node_name, field_name)

print arcpy.GetMessages()

except Exception as e:

print(e)

finally:

print('Success!')

但是為了和公司保持統(tǒng)一,于是使用java基于geotools又實(shí)現(xiàn)了一遍,可視化界面的代碼就不貼了,就放一些節(jié)點(diǎn)提取的代碼,以作備份。

/**

* 道路結(jié)點(diǎn)提取

*

* @param fileName

*/

private void extractNode(String fileName) throws IOException {

if (Strings.isNullOrEmpty(fileName)) return;

String temp[] = fileName.split("\\\\");

String shpName = "";

if (temp.length > 1) {

for (int j = 0; j < temp.length - 1; j++) {

shpName = shpName + temp[j] + "\\";

}

}

String shpFileName = shpName + "Node.shp";

File newFile =new File(shpFileName);

//設(shè)置要生成的shp文件的屬性

//下面是定義要素的字段(屬性)

//第一個(gè)參數(shù)是要素類型,第二個(gè)參數(shù)是字段名

//下面對(duì)應(yīng)SHP文件的dbf表中的Shape、name和number字段,FID字段默認(rèn)生成

//其中srid=4326是定義地理坐標(biāo)系WGS_84,與ESRI的WKID一樣,因?yàn)槎际荗GC定義的

SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();

tb.setCRS(DefaultGeographicCRS.WGS84);

tb.setName("shapefile");

tb.add("the_geom", com.vividsolutions.jts.geom.Point.class);

tb.add("NODEID", Long.class);

//SHP數(shù)據(jù)存儲(chǔ)工廠

ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();

//定義生成時(shí)的屬性

Map params = new HashMap();

params.put("url", newFile.toURI().toURL());

params.put("create spatial index", Boolean.TRUE);

//生成SHP

ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);

newDataStore.createSchema(tb.buildFeatureType());

newDataStore.setCharset(Charset.forName("GBK"));

//設(shè)置Writer

FeatureWriter writer = newDataStore.getFeatureWriter(newDataStore.getTypeNames()[0], Transaction.AUTO_COMMIT);

//加載shapefile

SimpleFeatureSource featureSource = loadShapeFile(fileName);

//檢查shapefile字段信息

checkShapeFileSchema(featureSource.getSchema(), fileName, "SNodeID","ENodeID");

try {

mainController.setStatus("正在進(jìn)行道路結(jié)點(diǎn)提取...");

String finalNodeID = null;

try {

//總筆數(shù)

int count = DataUtilities.count(featureSource.getFeatures());

Map map = new HashMap();

//逐筆寫入數(shù)據(jù)庫(kù)

try (SimpleFeatureIterator iterator = featureSource.getFeatures().features()) {

int index = 0;

while (iterator.hasNext()) {

SimpleFeature feature = iterator.next();

Object attribute = feature.getAttribute("SNodeID");

if (attribute != null && !Strings.isNullOrEmpty(attribute.toString()))

finalNodeID = attribute.toString();

Geometry geometry = getNode(feature,"start");

if (geometry != null) {

geometry.setSRID(SRID);

map.put(finalNodeID,geometry);

}

attribute = feature.getAttribute("ENodeID");

if (attribute != null && !Strings.isNullOrEmpty(attribute.toString()))

finalNodeID = attribute.toString();

geometry = getNode(feature,"end");

if (geometry != null) {

geometry.setSRID(SRID);

map.put(finalNodeID,geometry);

}

index++;

}

SimpleFeature newFeature = null;

Iterator iter = map.entrySet().iterator();

while (iter.hasNext()) {

newFeature = writer.next();

Map.Entry entry = (Map.Entry) iter.next();

String key = (String) entry.getKey();

Geometry geom = (Geometry) entry.getValue();

newFeature.setAttribute("NODEID",key);

newFeature.setAttribute("the_geom",geom);

}

writer.write();

writer.close();

newDataStore.dispose();

}

} catch (Exception e) {

throw new IllegalStateException(String.format("shapefile 文件: %s 數(shù)據(jù)處理出錯(cuò)!\n錯(cuò)誤信息: %s", fileName, e.getMessage()), e);

}

} finally {

mainController.setStatus(null);

mainController.setProgress(0);

featureSource.getDataStore().dispose();

}

}

/**

* 根據(jù)Feature要素獲取geometry并重新設(shè)置geometry

*

*/

private Geometry getNode(SimpleFeature feature,String str){

Geometry geo = (Geometry) feature.getDefaultGeometry();

if(geo == null)

return null;

if (geo == null) return null;

if(geo.getGeometryType().equals("LineString")){

return geometryFactory.createLineString(formatSingleGeometryCorrdinate(geo));

}else if(geo.getGeometryType().equals("MultiLineString")){

return geometryFactory.createPoint(formatMultiLineStringCorrdinate(geo,str));

}else {

return geo;

}

}

最終完成道路結(jié)點(diǎn)的提取,隨著需求越來(lái)越多,后期會(huì)逐步加深對(duì)geotools的研究。

總結(jié)

以上是生活随笔為你收集整理的python如何使用geotools_基于GeoTools实现道路结点的提取的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。