Skip to content

Commit

Permalink
adjust MediaWiki importer geo coordinate calculation
Browse files Browse the repository at this point in the history
- allow lat/long 0.xxx
- south / west assignment
include test class
  • Loading branch information
reger committed Oct 26, 2015
1 parent 2b775d5 commit d223cf0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
17 changes: 11 additions & 6 deletions source/net/yacy/data/wiki/WikiCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,11 @@ private String processLineOfWikiCode(final String hostport, String line) {
}


/**
* Process line with geo coordinate metadata
* @param line of wiki text
* @return line with geo coordinate formatted to be recogizeable by parser
*/
private static String processMetadata(String line) {
int p, q, s = 0;
while ((p = line.indexOf(WIKI_OPEN_METADATA, s)) >= 0 && (q = line.indexOf(WIKI_CLOSE_METADATA, p + 1)) >= 0) {
Expand All @@ -1041,7 +1046,7 @@ private static String processMetadata(String line) {
// {{Coordinate |NS 45/37/43.0/N |EW. 07/58/41.0/E |type=landmark |region=IT-BI}} ## means: degree/minute/second
// {{Coordinate |NS 51.48994 |EW. 7.33249 |type=landmark |region=DE-NW}}
final String b[] = a.split("\\|");
float lon = 0.0f, lat = 0.0f;
float lon = Float.NaN, lat = Float.NaN;
float lonm = 0.0f, latm = 0.0f;
String lono = "E", lato = "N";
String name = "";
Expand All @@ -1053,18 +1058,18 @@ private static String processMetadata(String line) {
final String d[] = c.substring(3).split("/");
if (d.length == 1) {float l = Float.parseFloat(d[0]); if (l < 0) {lato = "S"; l = -l;} lat = (float) Math.floor(l); latm = 60.0f * (l - lat);}
else if (d.length == 2) {lat = Float.parseFloat(d[0]); latm = Float.parseFloat(d[1]);}
else if (d.length == 3) {lat = Float.parseFloat(d[0]); latm = Float.parseFloat(d[1]) + Float.parseFloat(d[2]) / 60.0f;}
if (d[d.length-1].toUpperCase().equals("S")) {}
else if (d.length >= 3) {lat = Float.parseFloat(d[0]); latm = Float.parseFloat(d[1]) + Float.parseFloat(d[2]) / 60.0f;}
if (d[d.length-1].toUpperCase().equals("S")) {lato = "S";}
}
if (c.toUpperCase().startsWith("EW=")) {
final String d[] = c.substring(3).split("/");
if (d.length == 1) {float l = Float.parseFloat(d[0]); if (l < 0) {lono = "W"; l = -l;} lon = (float) Math.floor(l); lonm = 60.0f * (l - lon);}
else if (d.length == 2) {lon = Float.parseFloat(d[0]); lonm = Float.parseFloat(d[1]);}
else if (d.length == 3) {lon = Float.parseFloat(d[0]); lonm = Float.parseFloat(d[1]) + Float.parseFloat(d[2]) / 60.0f;}
if (d[d.length-1].toUpperCase().equals("W")) {lon = -lon; lonm = -lonm;}
else if (d.length >= 3) {lon = Float.parseFloat(d[0]); lonm = Float.parseFloat(d[1]) + Float.parseFloat(d[2]) / 60.0f;}
if (d[d.length-1].toUpperCase().equals("W")) {lato = "W";}
}
}
if (lon != 0.0d && lat != 0.0d) {
if (!Float.isNaN(lon) && !Float.isNaN(lat)) {
// replace this with a format that the html parser can understand
line = line.substring(0, p) + (name.length() > 0 ? (" " + name) : "") + " <nobr> " + lato + " " + lat + "\u00B0 " + latm + "'</nobr><nobr>" + lono + " " + lon + "\u00B0 " + lonm + "'</nobr> " + line.substring(q + WIKI_CLOSE_METADATA.length());
s = p;
Expand Down
35 changes: 35 additions & 0 deletions test/net/yacy/data/wiki/WikiCodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.yacy.data.wiki;

import org.junit.Test;
import static org.junit.Assert.*;


public class WikiCodeTest {

/**
* test geo location metadata convert
*/
@Test
public void testProcessMetadata() {
String[] testmeta = new String[]{
"{{coordinate|NS=52.205944|EW=0.117593|region=GB-CAM|type=landmark}}", // decimal N-E location
"{{coordinate|NS=43/50/29/N|EW=73/23/17/W|type=landmark|region=US-NY}}", // N-W location

"{{Coordinate |text=DMS |NS=50/7/49/N |EW=6/8/09/E |type=landmark |region=BE-WLG |name=Monument des trois Frontières}}",
"{{Coordinate |text=DMS |NS= 49.047169|EW=7.899148|region=DE-RP |type=landmark |name=Europadenkmal (Rheinland-Pfalz)}}",

"{{coordinate|NS=0.00000|EW=0.117593}}", // testing equator coord
"{{coordinate|NS=-10.00000|EW=-10.10000}}" // testing S-E location

};
WikiCode wc = new WikiCode();
for (int i = 0; i < testmeta.length; i++) {
String result = wc.transform("http://wiki:8080",testmeta[i]);
System.out.println(testmeta[i] + " --> " + result);
// simply check if replacement took place, if no coordinate recognized original string is just html encoded
assertFalse(result.contains("#124;")); // simple check - result not containing char code for "{",
assertFalse(result.contains("#125;")); // simple check - result not containing char code for "}"
}
}

}

0 comments on commit d223cf0

Please sign in to comment.