diff --git a/AdjustTime.ini b/AdjustTime.ini index 979341a..fae4bfb 100644 --- a/AdjustTime.ini +++ b/AdjustTime.ini @@ -1,11 +1,16 @@ #by AdjustTime -#Thu Jun 30 23:11:20 JST 2016 +#Sat Oct 29 09:48:44 JST 2016 GPX.BASETIME=EXIF_TIME -GPX.noFirstNode=ON +IMG.OUTPUT_EXIF=true +GPX.OUTPUT_SPEED=true +GPX.OUTPUT_WPT=false +GPX.noFirstNode=true +IMG.OUTPUT=true GPX.gpxSplit=ON -IMG.TIME=2016-01-03T15\:14\:04 -GPX.REUSE=ON -IMG.BASE_FILE=S2690002.JPG -IMG.SOURCE_FOLDER=/media/yuu/public/osm/GPXs/\u7DBE\u702C\u5E02\u5409\u5CA1_20160103 -GPX.SOURCE_FOLDER=/media/yuu/public/osm/GPXs/\u7DBE\u702C\u5E02\u5409\u5CA1_20160103 -IMG.OUTPUT_FOLDER=/media/yuu/public/osm/GPXs/\u7DBE\u702C\u5E02\u5409\u5CA1_20160103 +GPX.OVERWRITE_MAGVAR=true +IMG.TIME=2016-08-14T11\:45\:47 +GPX.REUSE=false +IMG.BASE_FILE=IMG_0182.jpg +IMG.SOURCE_FOLDER=/media/yuu/public/osm/GPXs/Taibousaki +GPX.SOURCE_FOLDER=/media/yuu/public/osm/GPXs/Taibousaki +IMG.OUTPUT_FOLDER=/media/yuu/public/osm/GPXs/Taibousaki diff --git a/importPicture/src/osm/jp/gpx/Complementation.java b/importPicture/src/osm/jp/gpx/Complementation.java index a70daaf..24a753c 100644 --- a/importPicture/src/osm/jp/gpx/Complementation.java +++ b/importPicture/src/osm/jp/gpx/Complementation.java @@ -1,13 +1,18 @@ package osm.jp.gpx; import java.text.ParseException; - import org.w3c.dom.Element; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; public class Complementation { + public static final Double R = (6378137D + 6356752.314D)/2D; // 6367444.657m + //public static final double dLat = 0.00453D; // 1km距離を表す緯度(差分) + //public static final double dLon = 0.005588D; // 1km距離を表す経度(差分) + + public TagTrkpt imaTag = null; + public TagTrkpt maeTag = null; + public static boolean param_GpxOutputSpeed = false; + public static boolean param_GpxOverwriteMagvar = false; + /** * @@ -23,98 +28,39 @@ * @throws ParseException */ public Complementation(Element imaE, Element maeE) throws ParseException { - // がなければ、 - // 直前の位置と、現在地から進行方向を求める - NodeList nodes3 = imaE.getChildNodes(); - magvar = null; - speed = null; - for (int i3=0; i3 < nodes3.getLength(); i3++) { - Node node4 = nodes3.item(i3); - String nodename = node4.getNodeName().toLowerCase(); - if (nodename.equals("magvar")) { - magvar = (Element)node4; - break; - } - else if (nodename.equals("speed")) { - speed = (Element)node4; - break; - } - else if (nodename.equals("time")) { - String timeStr = ((Element)node4).getTextContent(); - setImaTIME(ImportPicture.dfuk.parse(timeStr).getTime()); - break; - } - } - - NamedNodeMap nodeMap = imaE.getAttributes(); - for (int j=0; j < nodeMap.getLength(); j++ ) { - switch (nodeMap.item(j).getNodeName()) { - case "lat": - String latStr = nodeMap.item(j).getNodeValue(); - imaLAT = new Double(latStr); - break; - case "lon": - String lonStr = nodeMap.item(j).getNodeValue(); - imaLON = new Double(lonStr); - break; - } - } - + this.imaTag = new TagTrkpt(imaE); if (maeE != null) { - nodes3 = maeE.getChildNodes(); - for (int i3=0; i3 < nodes3.getLength(); i3++) { - Node node4 = nodes3.item(i3); - if (node4.getNodeName().toLowerCase().equals("time")) { - String timeStr = node4.getTextContent(); - setMaeTIME(ImportPicture.dfuk.parse(timeStr).getTime()); - break; - } - } - - nodeMap = maeE.getAttributes(); - for (int j=0; j < nodeMap.getLength(); j++ ) { - switch (nodeMap.item(j).getNodeName()) { - case "lat": - String latStr = nodeMap.item(j).getNodeValue(); - maeLAT = new Double(latStr); - break; - case "lon": - String lonStr = nodeMap.item(j).getNodeValue(); - maeLON = new Double(lonStr); - break; - } - } + this.maeTag = new TagTrkpt(maeE); } } - public static final Double R = (6378137D + 6356752.314D)/2D; // 6367444.657m - //public static final double dLat = 0.00453D; // 1km距離を表す緯度(差分) - //public static final double dLon = 0.005588D; // 1km距離を表す経度(差分) - - public long imaTIME = 0L; - public long maeTIME = 0L; - public Double maeLAT = null; - public Double maeLON = null; - public Double imaLAT = null; - public Double imaLON =null; - public Element magvar = null; - public Element speed = null; - /** * 緯度・経度と時間差から速度(m/sec)を求める * */ - public void complementationSpeed(Element imaE) throws ParseException { - Element speed = imaE.getOwnerDocument().createElement("speed"); - double d = Coords.calcDistHubeny(imaLAT, imaLON, maeLAT, maeLON); - String str = Double.toString((d * 1000) / (imaTIME - maeTIME)); - int iDot = str.indexOf('.'); - if (iDot > 0) { - str = str.substring(0, iDot+2); - } - speed.setTextContent(str); - imaE.appendChild(speed); + public void complementationSpeed() { + if (imaTag.speedStr != null) { + try { + Double.parseDouble(imaTag.speedStr); + } + catch (NumberFormatException e) { + // 数字以外ならエレメントを削除する + imaTag.removeElement("speed"); + imaTag.speedStr = null; + } + } + + if (imaTag.speedStr == null) { + double d = Coords.calcDistHubeny(imaTag.lat, imaTag.lon, maeTag.lat, maeTag.lon); + String str = Double.toString((d * 1000) / (imaTag.time.getTime() - maeTag.time.getTime())); + int iDot = str.indexOf('.'); + if (iDot > 0) { + str = str.substring(0, iDot+2); + } + imaTag.appendElement("speed", str); + imaTag.speedStr = new String(str); + } } /** @@ -128,90 +74,48 @@ * @param maeTIME * @throws ParseException */ - public void complementationMagvar(Element imaE) throws ParseException { - Double r = Math.cos(Math.toRadians((imaLAT + maeLAT) / 2)) * R; - Double x = Math.toRadians(imaLON - maeLON) * r; - Double y = Math.toRadians(imaLAT - maeLAT) * R; - double rad = Math.toDegrees(Math.atan2(y, x)); - - if (y >= 0) { - if (x >= 0) { - rad = 0 - (rad - 90); + public void complementationMagvar() throws ParseException { + if (imaTag.magvarStr != null) { + try { + Double.parseDouble(imaTag.magvarStr); + } + catch (NumberFormatException e) { + // 数字以外ならエレメントを削除する + imaTag.removeElement("magvar"); + imaTag.magvarStr = null; + } + } + + if (imaTag.magvarStr == null) { + Double r = Math.cos(Math.toRadians((imaTag.lat + maeTag.lat) / 2)) * R; + Double x = Math.toRadians(imaTag.lon - maeTag.lon) * r; + Double y = Math.toRadians(imaTag.lat - maeTag.lat) * R; + double rad = Math.toDegrees(Math.atan2(y, x)); + + if (y >= 0) { + if (x >= 0) { + rad = 0 - (rad - 90); + } + else { + rad = 360 - (rad - 90); + } } else { - rad = 360 - (rad - 90); + if (x >= 0) { + rad = 90 - rad; + } + else { + rad = 90 - rad; + } } - } - else { - if (x >= 0) { - rad = 90 - rad; - } - else { - rad = 90 - rad; - } - } - Element magvar = imaE.getOwnerDocument().createElement("magvar"); - String str = Double.toString(rad); - int iDot = str.indexOf('.'); - if (iDot > 0) { - str = str.substring(0, iDot); - } - magvar.setTextContent(str); - imaE.appendChild(magvar); + String str = Double.toString(rad); + int iDot = str.indexOf('.'); + if (iDot > 0) { + str = str.substring(0, iDot); + } + imaTag.appendElement("magvar", str); + imaTag.magvarStr = new String(str); + } } - - public long getImaTIME() { - return imaTIME; - } - - public void setImaTIME(long imaTIME) { - this.imaTIME = imaTIME; - } - - public long getMaeTIME() { - return maeTIME; - } - - public void setMaeTIME(long maeTIME) { - this.maeTIME = maeTIME; - } - - public Double getMaeLAT() { - return maeLAT; - } - - public void setMaeLAT(Double maeLAT) { - this.maeLAT = maeLAT; - } - - public Double getMaeLON() { - return maeLON; - } - - public void setMaeLON(Double maeLON) { - this.maeLON = maeLON; - } - - public int getMagvar() { - String magvarStr = magvar.getTextContent(); - return Integer.getInteger(magvarStr); - } - - public Double getImaLAT() { - return imaLAT; - } - - public void setImaLAT(Double imaLAT) { - this.imaLAT = imaLAT; - } - - public Double getImaLON() { - return imaLON; - } - - public void setImaLON(Double imaLON) { - this.imaLON = imaLON; - } - } diff --git a/importPicture/src/osm/jp/gpx/ImportPicture.java b/importPicture/src/osm/jp/gpx/ImportPicture.java index c28da45..c03faaf 100644 --- a/importPicture/src/osm/jp/gpx/ImportPicture.java +++ b/importPicture/src/osm/jp/gpx/ImportPicture.java @@ -184,12 +184,12 @@ paramStr = obj.params.getProperty(AppParameters.GPX_OVERWRITE_MAGVAR); if ((paramStr != null) && (paramStr.equals(Boolean.toString(true)))) { - obj.param_GpxOverwriteMagvar = true; + Complementation.param_GpxOverwriteMagvar = true; } paramStr = obj.params.getProperty(AppParameters.GPX_OUTPUT_SPEED); if ((paramStr != null) && (paramStr.equals(Boolean.toString(true)))) { - obj.param_GpxOutputSpeed = true; + Complementation.param_GpxOutputSpeed = true; } paramStr = obj.params.getProperty(AppParameters.GPX_SOURCE_FOLDER); @@ -246,8 +246,8 @@ System.out.println(" - param: "+ AppParameters.IMG_OUTPUT_EXIF +"= "+ String.valueOf(obj.exif)); System.out.println(" - param: "+ AppParameters.GPX_SOURCE_FOLDER +"="+ obj.param_GpxSourceFolder); System.out.println(" - param: "+ AppParameters.GPX_OUTPUT_WPT +"="+ obj.param_GpxOutputWpt); - System.out.println(" - param: "+ AppParameters.GPX_OVERWRITE_MAGVAR +"="+ obj.param_GpxOverwriteMagvar); - System.out.println(" - param: "+ AppParameters.GPX_OUTPUT_SPEED +"="+ obj.param_GpxOutputSpeed); + System.out.println(" - param: "+ AppParameters.GPX_OVERWRITE_MAGVAR +"="+ Complementation.param_GpxOverwriteMagvar); + System.out.println(" - param: "+ AppParameters.GPX_OUTPUT_SPEED +"="+ Complementation.param_GpxOutputSpeed); obj.start(); try { obj.join(); @@ -265,10 +265,11 @@ public boolean param_GpxSplit = false; public boolean param_GpxNoFirstNode = false; public boolean param_GpxReuse = false; - public boolean param_GpxOutputSpeed = false; - public boolean param_GpxOverwriteMagvar = false; + //public boolean Complementation.param_GpxOutputSpeed = false; + //public boolean Complementation.param_GpxOverwriteMagvar = false; public boolean param_GpxOutputWpt = true; public String param_GpxSourceFolder = "."; + public static final long DIFF_MAE_TIME = 3000L; // before 3 secound @Override public void run() { @@ -357,7 +358,7 @@ TreeMap map = new TreeMap<>(); TreeMap mapTRKSEG = new TreeMap<>(); Element trk = null; - Element maeTRKPT = null; + //Element maeTRKPT = null; gpx = builder.parse(gpxFile).getFirstChild(); Document doc = gpx.getOwnerDocument(); NodeList nodes = gpx.getChildNodes(); @@ -381,26 +382,6 @@ if (param_GpxNoFirstNode && (i2 == 0)) { continue; } - if (param_GpxOutputSpeed || param_GpxOverwriteMagvar) { - Complementation cmp = new Complementation((Element)nodeTRKPT, maeTRKPT); - if (param_GpxOutputSpeed) { - if (cmp.speed != null) { - nodeTRKPT.removeChild(cmp.speed); - } - if (maeTRKPT != null) { - cmp.complementationSpeed((Element)nodeTRKPT); - } - } - if (param_GpxOverwriteMagvar) { - if (cmp.magvar != null) { - nodeTRKPT.removeChild(cmp.magvar); - } - if (maeTRKPT != null) { - cmp.complementationMagvar((Element)nodeTRKPT); - } - } - } - maeTRKPT = getCopy(doc, nodeTRKPT); newTRKSEG.appendChild(getCopy(doc, nodeTRKPT)); } } @@ -523,40 +504,36 @@ } // 時刻uktimeにおけるをtrkptに追加する - Element trkpt = trkpt(map, uktime); - if (trkpt == null) { + Element trkptE = trkpt(map, uktime); + if (trkptE == null) { System.out.println(String.format("%20s ", "Out of GPX logging time.")); continue; } - - Element wpt = createWptTag(image, uktime.getTime(), trkpt); - String latStr = wpt.getAttribute("lat"); - String lonStr = wpt.getAttribute("lon"); - double latitude = Double.parseDouble(latStr); - double longitude = Double.parseDouble(lonStr); + + TagTrkpt trkptT = new TagTrkpt(trkptE); + + //Element wpt = createWptTag(image, uktime.getTime(), trkptE); + //String latStr = trkptT.lat.toString(); + //String lonStr = trkptT.lon.toString(); + double latitude = trkptT.lat; + double longitude = trkptT.lon; String eleStr = "-"; + if (trkptT.eleStr != null) { + eleStr = new String(trkptT.eleStr); + } + String magvarStr = "-"; + if (trkptT.magvarStr != null) { + magvarStr = new String(trkptT.magvarStr); + } + String speedStr = "-"; - NodeList nodes = wpt.getChildNodes(); // 子ノードを取得 - for (int i4 = 0; i4 < nodes.getLength(); i4++) { - Node node = nodes.item(i4); - if (node != null) { - switch (node.getNodeName()) { - case "ele": - eleStr = node.getFirstChild().getNodeValue(); - break; - case "magvar": - magvarStr = node.getFirstChild().getNodeValue(); - break; - case "speed": - speedStr = node.getFirstChild().getNodeValue(); - break; - } - } + if (trkptT.speedStr != null) { + speedStr = new String(trkptT.speedStr); } - System.out.print(String.format("%12s %12s|", latStr, lonStr)); + System.out.print(String.format("%12s %12s|", (new Double(latitude)).toString(), (new Double(longitude)).toString())); System.out.println(String.format("%8s|%6s|%s|", eleStr, magvarStr, speedStr)); ret = true; @@ -675,7 +652,7 @@ } if (Boolean.parseBoolean(params.getProperty(AppParameters.GPX_OUTPUT_WPT))) { - Element temp = getCopy(gpx.getOwnerDocument(), wpt); + Element temp = getCopy(gpx.getOwnerDocument(), trkptT.trkpt); gpx.appendChild(temp); } } @@ -744,16 +721,51 @@ * <ele>47.20000076293945</ele> * <time>2012-06-15T03:00:29Z</time> * </trkpt> + *DIFF_MAE_TIME * @return * @param map * @param jptime 画像ファイルの撮影日時 ミリ秒(日本時間) * @throws ParseException */ public Element trkpt(TreeMap map, Date jptime) throws ParseException { + // 指定した時刻のエレメント(imaTrkpt)を取得する + Element imaE = getTrkpt(map, jptime); + if (imaE != null) { + Element maeE = getMaeTrkpt(map, new TagTrkpt(imaE)); + if (maeE != null) { + Complementation comp = new Complementation(imaE, maeE); + + // がなければ、 + // 直前の位置と、現在地から進行方向を求める + // 経度(longitude)と経度から進行方向を求める + if (Complementation.param_GpxOverwriteMagvar) { + comp.complementationMagvar(); + } + + // 緯度・経度と時間差から速度(m/sec)を求める + if (Complementation.param_GpxOutputSpeed) { + comp.complementationSpeed(); + } + imaE = (Element)(comp.imaTag.trkpt.cloneNode(true)); + } + } + return imaE; + } + + /** + * [map]から指定した時刻のエレメントを取り出す。 + * GPX時刻との差が10分以上は無効 + * + * @param map + * @param jptime + * @return + * @throws ParseException + */ + public Element getTrkpt(TreeMap map, Date jptime) throws ParseException { long sa = 2L * 3600000L; long jpt = jptime.getTime(); + Element ret = null; - Element mae = null; Set keySet = map.keySet(); //すべてのキー値を取得 Iterator keyIte = keySet.iterator(); @@ -764,26 +776,7 @@ if (Math.abs(jpt - t) < sa) { sa = Math.abs(jpt - t); ret = map.get(time); - - // がなければ、 - // 直前の位置と、現在地から進行方向を求める - Element magvar = null; - - if (mae != null) { - Complementation obj = new Complementation(ret, mae); - - // 経度(longitude)と経度から進行方向を求める - if ((magvar == null) || param_GpxOverwriteMagvar) { - obj.complementationMagvar(ret); - } - - // 緯度・経度と時間差から速度(m/sec)を求める - obj.complementationSpeed(ret); - - magvar = obj.magvar; - } } - mae = map.get(time); } if (sa < (60000L * 10L)) { @@ -793,7 +786,33 @@ return null; } + public Element getMaeTrkpt(TreeMap map, TagTrkpt imaTrkpt) throws ParseException { + Element ret = null; + long diffTime = 2L * 3600000L; // 2時間 + long jpt = imaTrkpt.time.getTime() - DIFF_MAE_TIME; + Set keySet = map.keySet(); //すべてのキー値を取得 + Iterator keyIte = keySet.iterator(); + while (keyIte.hasNext()) { //ループ。反復子iteratorによる キー 取得 + Long time = keyIte.next(); + long t = time; + + if (Math.abs(jpt - t) < diffTime) { + diffTime = Math.abs(jpt - t); + ret = map.get(time); + } + } + + if (diffTime < (60000L * 10L)) { + // GPX時刻との差が10分以内なら有効 + if (diffTime < (imaTrkpt.time.getTime() - 1000)) { + // 元の時刻との差が1秒以上あること + return ret; + } + } + return null; + } + /** * 対象は '*.JPG' のみ対象とする * @return diff --git a/importPicture/src/osm/jp/gpx/TagTrkpt.java b/importPicture/src/osm/jp/gpx/TagTrkpt.java new file mode 100644 index 0000000..453adf5 --- /dev/null +++ b/importPicture/src/osm/jp/gpx/TagTrkpt.java @@ -0,0 +1,122 @@ +package osm.jp.gpx; + +import java.text.ParseException; +import java.util.Date; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * + * <trkpt lat="35.32123832" lon="139.56965631"> + * 47.20000076293945 + * + * + * + * </trkpt> + * + * @author yuu + * + */ +public class TagTrkpt { + public Element trkpt = null; + public Double lat = null; + public Double lon = null; + public String eleStr = null; + public Date time = null; + public String magvarStr = null; + public String speedStr = null; + + public TagTrkpt(Element trkpt) { + this.trkpt = (Element) trkpt.cloneNode(true); + + NamedNodeMap nodeMap = trkpt.getAttributes(); + for (int j=0; j < nodeMap.getLength(); j++ ) { + switch (nodeMap.item(j).getNodeName()) { + case "lat": + String latStr = nodeMap.item(j).getNodeValue(); + this.lat = new Double(latStr); + break; + case "lon": + String lonStr = nodeMap.item(j).getNodeValue(); + this.lon = new Double(lonStr); + break; + } + } + + NodeList nodes1 = trkpt.getChildNodes(); + for (int i1=0; i1 < nodes1.getLength(); i1++) { + Node node1 = nodes1.item(i1); + NodeList nodes2 = node1.getChildNodes(); + switch (node1.getNodeName()) { + case "ele": + for (int i2=0; i2 < nodes2.getLength(); i2++) { + Node node2 = nodes2.item(i2); + if (node2 != null) { + if (node2.getNodeType() == Node.TEXT_NODE) { + this.eleStr = node2.getNodeValue(); + } + } + } + break; + case "time": + for (int i2=0; i2 < nodes2.getLength(); i2++) { + Node node2 = nodes2.item(i2); + if (node2 != null) { + if (node2.getNodeType() == Node.TEXT_NODE) { + try { + this.time = ImportPicture.dfuk.parse(node2.getNodeValue()); + } catch (ParseException e) { + this.time = null; + } + } + } + } + break; + case "magvar": + for (int i2=0; i2 < nodes2.getLength(); i2++) { + Node node2 = nodes2.item(i2); + if (node2 != null) { + if (node2.getNodeType() == Node.TEXT_NODE) { + this.magvarStr = node2.getNodeValue(); + } + } + } + break; + case "speed": + for (int i2=0; i2 < nodes2.getLength(); i2++) { + Node node2 = nodes2.item(i2); + if (node2 != null) { + if (node2.getNodeType() == Node.TEXT_NODE) { + this.speedStr = node2.getNodeValue(); + } + } + } + break; + } + } + } + + public void removeElement(String eleName) { + Node child; + for (child = trkpt.getFirstChild(); child != null; child = child.getNextSibling()) { + NodeList nodeList = child.getChildNodes(); + for(int i = 0; i < nodeList.getLength(); i++) { + Node grandChild = child.getChildNodes().item(i); + if (grandChild.getNodeName().equals(eleName)) { + child.removeChild(grandChild); + } + } + } + } + + public void appendElement(String eleName, String valueStr) { + Document doc = trkpt.getOwnerDocument(); + Element newElement = doc.createElement(eleName); + newElement.setTextContent(valueStr); + trkpt.appendChild(newElement); + } +}