Skip to content

Commit

Permalink
Finish transition to Fieldpapers and new scrape logic for WP.org
Browse files Browse the repository at this point in the history
  • Loading branch information
iandees committed Jul 16, 2013
1 parent b2ef175 commit 9aecb2a
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry combineaccessrules="false" kind="src" path="/JOSM"/>
<classpathentry combineaccessrules="false" kind="src" path="/josm"/>
<classpathentry kind="output" path="build"/>
</classpath>
7 changes: 4 additions & 3 deletions README
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
A plugin for displaying tiled, scanned maps from walking-papers.org.
A plugin for displaying tiled, scanned maps from fieldpapers.org.

Written by Frederik Ramm <[email protected]>, based on SlippyMap
plugin work by Lubomir Varga <[email protected]> or <[email protected]>.
Adapted by Ian Dees <[email protected]> from a plugin written by
Frederik Ramm <[email protected]>, based on SlippyMap plugin work
by Lubomir Varga <[email protected]> or <[email protected]>.

Public Domain.
169 changes: 161 additions & 8 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,173 @@
** Call "ant help" to get possible build targets.
**
-->
<project name="walkingpapers" default="dist" basedir=".">
<project name="fieldpapers" default="dist" basedir=".">

<!-- enter the SVN commit message -->
<property name="commit.message" value="Changed the constructor signature of the plugin main class"/>
<!-- enter the *lowest* JOSM version this plugin is currently compatible with -->
<property name="plugin.main.version" value="4549"/>

<property name="plugin.author" value="Frederik Ram"/>
<property name="plugin.class" value="org.openstreetmap.josm.plugins.walkingpapers.WalkingPapersPlugin"/>
<property name="plugin.description" value="Supports downloading tiled, scanned maps from walking-papers.org. This plugin is still under early development and may be buggy."/>
<property name="plugin.icon" value="images/preferences/walkingpapers.png"/>
<property name="plugin.link" value="http://wiki.openstreetmap.org/index.php/JOSM/Plugins/WalkingPapers"/>
<property name="plugin.author" value="Ian Dees"/>
<property name="plugin.class" value="org.openstreetmap.josm.plugins.fieldpapers.FieldPapersPlugin"/>
<property name="plugin.description" value="Supports downloading tiled, scanned maps from fieldpapers.org."/>
<property name="plugin.icon" value="images/preferences/fieldpapers.png"/>
<property name="plugin.link" value="http://wiki.openstreetmap.org/index.php/JOSM/Plugins/FieldPapers"/>

<!-- ** include targets that all plugins have in common ** -->
<import file="../build-common.xml"/>
<property name="josm" location="../../core/dist/josm-custom.jar"/>
<property name="plugin.dist.dir" value="../../dist"/>
<property name="plugin.build.dir" value="build"/>
<property name="plugin.jar" value="${plugin.dist.dir}/${ant.project.name}.jar"/>
<property name="ant.build.javac.target" value="1.5"/>
<target name="init">
<mkdir dir="${plugin.build.dir}"/>
</target>
<target name="compile" depends="init">
<echo message="creating ${plugin.jar}"/>
<javac srcdir="src" classpath="${josm}" debug="true" destdir="${plugin.build.dir}">
<compilerarg value="-Xlint:deprecation"/>
<compilerarg value="-Xlint:unchecked"/>
</javac>
</target>
<target name="dist" depends="compile">
<copy todir="${plugin.build.dir}/images">
<fileset dir="images"/>
</copy>
<copy todir="${plugin.build.dir}/data">
<fileset dir="data"/>
</copy>
<jar destfile="${plugin.jar}" basedir="${plugin.build.dir}">
<manifest>
<attribute name="Author" value="${plugin.author}"/>
<attribute name="Plugin-Class" value="${plugin.class}"/>
<attribute name="Plugin-Date" value="${version.entry.commit.date}"/>
<attribute name="Plugin-Description" value="${plugin.description}"/>
<attribute name="Plugin-Icon" value="${plugin.icon}"/>
<attribute name="Plugin-Link" value="${plugin.link}"/>
<attribute name="Plugin-Mainversion" value="${plugin.main.version}"/>
<attribute name="Plugin-Version" value="${version.entry.commit.revision}"/>
</manifest>
</jar>
</target>
<target name="revision">
<exec executable="git" outputproperty="git.revision" failifexecutionfails="false" errorproperty="">
<arg value="describe"/>
<arg value="--tags"/>
<arg value="--always"/>
<arg value="HEAD"/>
</exec>
<condition property="repository.version" value="${git.revision}" else="unknown">
<and>
<isset property="git.revision"/>
<length string="${git.revision}" trim="yes" length="0" when="greater"/>
</and>
</condition>
</target>
<target name="clean">
<delete dir="${plugin.build.dir}"/>
<delete file="${plugin.jar}"/>
</target>
<target name="install" depends="dist">
<property environment="env"/>
<condition property="josm.plugins.dir" value="${env.APPDATA}/JOSM/plugins" else="${user.home}/.josm/plugins">
<and>
<os family="windows"/>
</and>
</condition>
<copy file="${plugin.jar}" todir="${josm.plugins.dir}"/>
</target>
<!--
************************** Publishing the plugin ***********************************
-->
<!--
** extracts the JOSM release for the JOSM version in ../core and saves it in the
** property ${coreversion.info.entry.revision}
**
-->
<target name="core-info">
<exec append="false" output="core.info.xml" executable="svn" failifexecutionfails="false">
<env key="LANG" value="C"/>
<arg value="info"/>
<arg value="--xml"/>
<arg value="../../core"/>
</exec>
<xmlproperty file="core.info.xml" prefix="coreversion" keepRoot="true" collapseAttributes="true"/>
<echo>Building against core revision ${coreversion.info.entry.revision}.</echo>
<echo>Plugin-Mainversion is set to ${plugin.main.version}.</echo>
<delete file="core.info.xml"/>
</target>
<!--
** commits the source tree for this plugin
-->
<target name="commit-current">
<echo>Commiting the plugin source with message '${commit.message}' ...</echo>
<exec append="true" output="svn.log" executable="svn" failifexecutionfails="false">
<env key="LANG" value="C"/>
<arg value="commit"/>
<arg value="-m '${commit.message}'"/>
<arg value="."/>
</exec>
</target>
<!--
** updates (svn up) the source tree for this plugin
-->
<target name="update-current">
<echo>Updating plugin source ...</echo>
<exec append="true" output="svn.log" executable="svn" failifexecutionfails="false">
<env key="LANG" value="C"/>
<arg value="up"/>
<arg value="."/>
</exec>
<echo>Updating ${plugin.jar} ...</echo>
<exec append="true" output="svn.log" executable="svn" failifexecutionfails="false">
<env key="LANG" value="C"/>
<arg value="up"/>
<arg value="../dist/${plugin.jar}"/>
</exec>
</target>
<!--
** commits the plugin.jar
-->
<target name="commit-dist">
<echo>
***** Properties of published ${plugin.jar} *****
Commit message : '${commit.message}'
Plugin-Mainversion: ${plugin.main.version}
JOSM build version: ${coreversion.info.entry.revision}
Plugin-Version : ${version.entry.commit.revision}
***** / Properties of published ${plugin.jar} *****

Now commiting ${plugin.jar} ...
</echo>
<exec append="true" output="svn.log" executable="svn" failifexecutionfails="false">
<env key="LANG" value="C"/>
<arg value="-m '${commit.message}'"/>
<arg value="commit"/>
<arg value="${plugin.jar}"/>
</exec>
</target>
<!-- ** make sure svn is present as a command line tool ** -->
<target name="ensure-svn-present">
<exec append="true" output="svn.log" executable="svn" failifexecutionfails="false" failonerror="false" resultproperty="svn.exit.code">
<env key="LANG" value="C"/>
<arg value="--version"/>
</exec>
<fail message="Fatal: command 'svn --version' failed. Please make sure svn is installed on your system.">
<!-- return code not set at all? Most likely svn isn't installed -->
<condition>
<not>
<isset property="svn.exit.code"/>
</not>
</condition>
</fail>
<fail message="Fatal: command 'svn --version' failed. Please make sure a working copy of svn is installed on your system.">
<!-- error code from SVN? Most likely svn is not what we are looking on this system -->
<condition>
<isfailure code="${svn.exit.code}"/>
</condition>
</fail>
</target>
<target name="publish" depends="ensure-svn-present,core-info,commit-current,update-current,clean,dist,commit-dist">
</target>

</project>
File renamed without changes
File renamed without changes
Original file line number Diff line number Diff line change
Expand Up @@ -20,79 +20,82 @@
public class FieldPapersAddLayerAction extends JosmAction {

public FieldPapersAddLayerAction() {
super(tr("Scanned Map..."), "walkingpapers",
tr("Display a map that was previously scanned and uploaded to walking-papers.org"), null, false);
super(tr("Scanned Map..."), "fieldpapers",
tr("Display a map that was previously scanned and uploaded to fieldpapers.org"), null, false);
}

public void actionPerformed(ActionEvent e) {
String wpid = JOptionPane.showInputDialog(Main.parent,
tr("Enter a walking-papers.org URL or ID (the bit after the ?id= in the URL)"),
Main.pref.get("walkingpapers.last-used-id"));
tr("Enter a fieldpapers.org snapshot URL"),
Main.pref.get("fieldpapers.last-used-id"));

if (wpid == null || wpid.equals("")) return;

// Grab id= from the URL if we need to, otherwise get an ID
String mungedWpId = this.getWalkingPapersId(wpid);
String mungedWpId = this.getFieldPapersId(wpid);

if (mungedWpId == null || mungedWpId.equals("")) return;

// screen-scrape details about this id from walking-papers.org
String wpUrl = Main.pref.get("walkingpapers.base-url", "http://walking-papers.org/") + "scan.php?id=" + mungedWpId;
// screen-scrape details about this id from fieldpapers.org
// e.g. http://fieldpapers.org/snapshot.php?id=nq78w6wl
String wpUrl = Main.pref.get("fieldpapers.base-url", "http://fieldpapers.org/") + "snapshot.php?id=" + mungedWpId;

Pattern spanPattern = Pattern.compile("<span class=\"(\\S+)\">(\\S+)</span>");
Pattern spanPattern = Pattern.compile("var (\\S*) = (\\S*);");
Matcher m;

String boundsStr = "";
String urlBase = null;
double north = 0;
double south = 0;
double east = 0;
double west = 0;
int minz = -1;
int maxz = -1;
String tile = null;

try {
BufferedReader r = new BufferedReader(new InputStreamReader(new URL(wpUrl).openStream(), "utf-8"));
for (String line = r.readLine(); line != null; line = r.readLine()) {
m = spanPattern.matcher(line);
if (m.find()) {
if ("tile".equals(m.group(1))) tile = m.group(2);
else if ("north".equals(m.group(1))) north = Double.parseDouble(m.group(2));
else if ("south".equals(m.group(1))) south = Double.parseDouble(m.group(2));
else if ("east".equals(m.group(1))) east = Double.parseDouble(m.group(2));
else if ("west".equals(m.group(1))) west = Double.parseDouble(m.group(2));
else if ("minzoom".equals(m.group(1))) minz = Integer.parseInt(m.group(2));
else if ("maxzoom".equals(m.group(1))) maxz = Integer.parseInt(m.group(2));
if ("geojpeg_bounds".equals(m.group(1))) boundsStr = m.group(2);
else if ("base_provider".equals(m.group(1))) urlBase = m.group(2);
}
}
r.close();
if ((tile == null) || (north == 0 && south == 0) || (east == 0 && west == 0)) throw new Exception();
if (!boundsStr.isEmpty()) {
String[] boundSplits = boundsStr.replaceAll("\"", "").split(",");
south = Double.parseDouble(boundSplits[0]);
west = Double.parseDouble(boundSplits[1]);
north = Double.parseDouble(boundSplits[2]);
east = Double.parseDouble(boundSplits[3]);
}

if (!urlBase.isEmpty()) {
urlBase = urlBase.replaceAll("\\\\", "").replaceAll("\"", "");
}

if (urlBase == null || (north == 0 && south == 0) || (east == 0 && west == 0)) throw new Exception();
} catch (Exception ex) {
JOptionPane.showMessageDialog(Main.parent,tr("Could not read information from walking-papers.org the id \"{0}\"", mungedWpId));
ex.printStackTrace();
JOptionPane.showMessageDialog(Main.parent,tr("Could not read information for the id \"{0}\" from fieldpapers.org", mungedWpId));
return;
}


//http://walking-papers.org/scan.php?id=rmvdr3lq
// The server is apparently broken and returning the WpId in the URL twice
// which makes it return errors when we fetch it. So, strip out one of
// the dups. This is a hack and needs to get removed when the server
// is fixed.
tile = tile.replaceFirst(mungedWpId+"/"+mungedWpId, mungedWpId);
Main.pref.put("walkingpapers.last-used-id", mungedWpId);
String tileUrl = urlBase + "/{zoom}/{x}/{y}.jpg";
Main.pref.put("fieldpapers.last-used-id", mungedWpId);

Bounds b = new Bounds(new LatLon(south, west), new LatLon(north, east));

FieldPapersLayer wpl = new FieldPapersLayer(mungedWpId, tile, b, minz, maxz);
FieldPapersLayer wpl = new FieldPapersLayer(mungedWpId, tileUrl, b, 13, 18);
Main.main.addLayer(wpl);

}

private static String getWalkingPapersId(String wpid) {
private String getFieldPapersId(String wpid) {
if (!wpid.contains("id=")) {
return wpid;
} else {
// To match e.g. http://walking-papers.org/scan.php?id=53h78bbx
final Pattern pattern = Pattern.compile("\\?id=(\\S+)");
// To match e.g. http://fieldpapers.org/snapshot.php?id=tz3fq6xl
// or http://fieldpapers.org/snapshot.php?id=nq78w6wl#15/41.8966/-87.6847
final Pattern pattern = Pattern.compile("snapshot.php\\?id=([a-z0-9]*)");
final Matcher matcher = pattern.matcher(wpid);
final boolean found = matcher.find();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public FieldPapersKey(int level, int x, int y) {
this.level = level;
if (level <= 0 || x < 0 || y < 0) {
this.valid = false;
System.err.println("invalid WalkingPapersKey("+level+", "+x+", "+y+")");
System.err.println("invalid FieldPapersKey("+level+", "+x+", "+y+")");
} else {
this.valid = true;
}
Expand Down Expand Up @@ -72,7 +72,7 @@ public int hashCode() {
*/
@Override
public String toString() {
return "WalkingPapersKey(x=" + this.x + ",y=" + this.y + ",level=" + level + ")";
return "FieldPapersKey(x=" + this.x + ",y=" + this.y + ",level=" + level + ")";
}

}
Loading

0 comments on commit 9aecb2a

Please sign in to comment.