📄 tracktold.java
字号:
package net.aetherial.gis.jiaotongbu.outputJTB.txtOutput.module.gpsdata.lxld.ld.
ldhf;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import net.aetherial.gis.surface.ItemValue;
import java.util.Vector;
import net.aetherial.gis.garmin.GarminGMLDoc;
import org.w3c.dom.Element;
import java.util.Set;
/**
* <p>Title:航迹变成路段 </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2004</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class TrackToLD {
public TrackToLD(Node trk) {
}
/**
* 从给定的路线编号 @param lxNumber
* 得到路线编码相同的 Node[]
*/
public static Node[] getSameLXNum(String lxNumber){
Node[] trks = ItemValue.getTracks();
Vector ve = new Vector();
if (trks != null) {
for (int i = 0; i < trks.length; i++) {
if ((ItemValue.getTracksNumber(trks[i])).equals(lxNumber)) {
ve.addElement(trks[i]);
}
}
}
Node[] temp = new Node[ve.size()];
for (int i = 0; i < temp.length; i++) {
temp[i] = (Node)ve.elementAt(i);
}
return temp;
}
/**
* 用航迹点切割航迹
* 产生两个Element 对象
*/
public static Vector cutByTrackPoint(Node trk,Node tp){
Vector result = null;
NodeList nl = ItemValue.getTracksPoint(trk);
double min = 9999999999999d;
int pos = 0;
for (int i = 0; i < nl.getLength() - 1; i++) {
Node nd1 = nl.item(i);
Node nd2 = nl.item(i + 1);
double distance = disance(tp, nd1, nd2);
if (distance < min) {
min = distance;
if(min < 0.5){
pos = i;
}
}
}
if(pos > 1){
Vector tk1 = new Vector();
Vector tk2 = new Vector();
for (int i = 0; i <= pos; i++) {
tk1.add(nl.item(i));
}
tk1.add(tp);
tk2.add(tp);
for (int i = pos + 1; i < nl.getLength(); i++) {
tk2.add(nl.item(i));
}
result = new Vector();
result.add(createNewTrack(trk, tk1));
result.add(createNewTrack(trk, tk2));
}
return result;
}
/**
* 根据给定的航迹点切割航迹
*/
public static Vector cutByTrackPoints(Vector trks,Vector tps){
// Vector result = new Vector();
Vector longerThan2KTKs = getLongerThan2KMTracks(trks);
while(longerThan2KTKs.size() > 0 && tps != null){
for (int i = 0; i < tps.size(); i++) {
Node tp = (Node) tps.get(i);
Vector temp = new Vector();
Node nearestTk = getNearestTrk(longerThan2KTKs, tp);
if (nearestTk != null) {
temp = cutByTrackPoint(nearestTk, tp);
if (temp != null) {
trks.remove(nearestTk);
ItemValue.deleteTracks(nearestTk);
for (int t = 0; t < temp.size(); t++) {
trks.add(temp.get(t));
}
tps.remove(tp);
}
// result.addAll(cutByTrackPoints(trks, tps));
longerThan2KTKs = getLongerThan2KMTracks(trks);
}
}
}
// }else if(trks != null){
// result.addAll(trks);
//// for(int i=0; i<trks.size(); i++){
//// result[i] = (Node)trks.get(i);
//// }
// }
return trks;
}
private static Node getNearestTrk(Vector tracks, Node tp){
Node result = null;
double min = 99999999999999d;
if(tracks != null && tracks.size() > 0){
for(int i=0; i<tracks.size(); i++){
NodeList tps = ItemValue.getTracksPoint( (Node) tracks.get(i));
double temp = 99999999999999d;
for(int j = 0; j < tps.getLength() - 1; j++){
Node nd1 = tps.item(j);
Node nd2 = tps.item(j+1);
double distance = disance(tp, nd1, nd2);
if(distance < temp) temp = distance;
// A2 = y4 - y3;
// B2 = x3 - x4;
// C2 = x4 * y3 - x3 * y4;
//
// A3 = z2 - z1;
// B3 = x1 - x2;
// C3 = x2 * z1 - x1 * z2;
//
// A4 = z4 - z3;
// B4 = x3 - x4;
// C4 = x4 * z3 - x3 * z4;
//
// x = (B1 * C2 - B2 * C1) / (A1 * B2 - A2 * B1);
// y = (A1 * C2 - A2 * C1) / (A1 * B2 - A2 * B1);
// z = ( -C3 - A3 * x) / B3;
}
if(temp < min) {
min = temp;
if(min < 0.5){
result = (Node) tracks.get(i);
}
}
}
}
return result;
}
private static double disance(Node tp, Node nd1, Node nd2) throws
NumberFormatException {
double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4;
double A1, B1, C1, A2, B2, C2, A3, B3, C3, A4, B4, C4;
x1 = Double.parseDouble(ItemValue.getTracksPointX(nd1));
y1 = Double.parseDouble(ItemValue.getTracksPointY(nd1));
z1 = Double.parseDouble(ItemValue.getTracksPointZ(nd1));
x2 = Double.parseDouble(ItemValue.getTracksPointX(nd2));
y2 = Double.parseDouble(ItemValue.getTracksPointY(nd2));
z2 = Double.parseDouble(ItemValue.getTracksPointZ(nd2));
x3 = Double.parseDouble(ItemValue.getTracksPointX(tp));
y3 = Double.parseDouble(ItemValue.getTracksPointY(tp));
z3 = Double.parseDouble(ItemValue.getTracksPointZ(tp));
A1 = y2 - y1;
B1 = x1 - x2;
C1 = x2 * y1 - x1 * y2;
// x = ( k^2 * pt1.x + k * (point.y - pt1.y ) + point.x ) / ( k^2 + 1) ,
// y = k * ( x - pt1.x) + pt1.y;
// k = ( pt2.y - pt1. y ) / (pt2.x - pt1.x )
double k = (y2-y1)/(x2-x1);
x = (k*k*x1*k*(y3-y1)+x3)/(k*k+1);
y = k*(x-x1)+y1;
double distance = Math.abs(A1*x3+B1*y3+C1)/Math.sqrt(A1*A1+B1*B1);
boolean inside = Math.min(x2,x1) < x && x < Math.max(x2,x1) && (Math.min(y2,y1) < y && y < Math.max(y2,y1));
if(! inside){
distance = Math.min(Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3)),Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)));
}
return distance;
}
private static Vector getLongerThan2KMTracks(Vector trks){
Vector result = new Vector();
for(int i = 0; i< trks.size(); i++){
if(ItemValue.getTracksDistance((Node)trks.get(i)) - 2*1000 > 0){
result.add(trks.get(i));
}
}
return result;
}
private static boolean isFindInTps(Node tp,Node[] trackPoints){
if (trackPoints == null) {
return false;
}else{
for (int i = 0; i < trackPoints.length; i++) {
if (trackPoints[i] == tp) {
return true;
}
}
}
return false;
}
/**
* 生成新的航迹
* 问题:ID会产生重复,已解决
*/
public static Element createNewTrack(Node sourceTrack,Vector children){
if(children.size()>1){
GarminGMLDoc.IDAadd();
Element e = GarminGMLDoc.addTrack(ItemValue.getTracksName(sourceTrack), sourceTrack);
for (int i = 0; i < children.size(); i++) {
GarminGMLDoc.addTrackPoint(e, (Node) children.elementAt(i));
}
ItemValue.setIDOrder();
return e;
}else{
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -