python如何使用geotools_基于GeoTools实现道路结点的提取
最近公司的地圖業(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 闲鱼上的转卖是什么意思
- 下一篇: python加油视频教程_TensorF