Why is this happening (strangeness with Processing and UnfoldingMaps)

632 views Asked by At

As part of the coursera UCSD Java class, we are working with the Processing and Unfolding Maps libraries.

The course provides you with starter code, which I am trying to expand. But I ran into a problem.

On my work computer, where I do most of my studying, I have a really nice app started. I decided to take it home to my wife to show her, by cloning my github repo to my home laptop.

None of the librar/JAR files were included, so I downloaded them directly from Processing and Unfolding's download pages. The result was a NIGHTMARE. Error after error after error. I assume coursera/UCSD used old versions of the libraries, and the new versions aren't backwards compatible. Seems crappy, but whatever.

Next, I downloaded all the JAR files from the coursera sight. The results are a bit better, but things are still pretty wacky. Namely, when I create an UnfoldingMap() object, the parameters for location within the window as well as size do absolutely nothing.

1.) I use Processing size() method to size up or down the containing window, it scales the map object up or down. It totally ignores the size and location arguments of UnfoldingMap().

2.) The map object is always pushed to the lower left hand corner of the window, no matter it's arguments for position.

3.) The gray background() I have chosen does show around the map, but the background itself is then surrounded by a big black field, that also scales with the size() parameters.

Here is some code:

package module3;

//Java utilities libraries
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//Processing library
import processing.core.PApplet;

//Unfolding libraries
import de.fhpotsdam.unfolding.UnfoldingMap;
import de.fhpotsdam.unfolding.marker.Marker;
import de.fhpotsdam.unfolding.data.PointFeature;
import de.fhpotsdam.unfolding.marker.SimplePointMarker;
import de.fhpotsdam.unfolding.providers.Google;
import de.fhpotsdam.unfolding.providers.MBTilesMapProvider;
import de.fhpotsdam.unfolding.utils.MapUtils;

//Parsing library
import parsing.ParseFeed;

public class EarthquakeCityMap extends PApplet {

    // Less than this threshold is a light earthquake
    public static final float THRESHOLD_MODERATE = 5;
    // Less than this threshold is a minor earthquake
    public static final float THRESHOLD_LIGHT = 4;

    // This is where to find the local tiles, for working without an
        // Internet connection 
    public static String mbTilesString = "blankLight-1-3.mbtiles";

    // The map
    private UnfoldingMap map;

    //feed with magnitude 2.5+ Earthquakes
    private String earthquakesURLweek = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.atom";
        private String earthquakesURLday = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.atom";

    public void setup() {
            size(400, 400, "processing.opengl.PGraphics3D");
            background(99);

            map = new UnfoldingMap(this, 50, 50, 1100, 700, new Google.GoogleMapProvider());
        map.zoomToLevel(2);
        MapUtils.createDefaultEventDispatcher(this, map);   

        //List of markers to be added to map
        List<Marker> markers = new ArrayList<Marker>();

        //Use parser to collect properties for each earthquake
        //PointFeatures have a getLocation method
        List<PointFeature> earthquakes = ParseFeed.parseEarthquake(this, earthquakesURLweek);

            // for each earthqauke feature, create a custom marker and add it
            // to the list.
            for (PointFeature quake : earthquakes){
                Marker quakeMark = createMarker(quake);
                markers.add(quakeMark);
                Map<String, Object> properties = quakeMark.getProperties();
            }

        // Add the markers to the map so that they are displayed
        map.addMarkers(markers);
    }

        /**
         * A helper method to style markers based on features (magnitude, depth,
         * etc) of an earthquake.
         * @param feature A PointFeature object representing a single earthquake.
         * @return 
         */
    private SimplePointMarker createMarker(PointFeature feature){  

            // Create a new SimplePointMarker at the location given by the PointFeature
            SimplePointMarker marker = new SimplePointMarker(feature.getLocation(), feature.getProperties());

            Object magObj = feature.getProperty("magnitude");
            Object ageObj =  marker.getProperty("days ellapsed");
            float mag = Float.parseFloat(magObj.toString());
            int age = (int) ageObj;

            //Set processing color and alpha data, for setting marker colors 
            //below.
            int alpha = 255 - (age * 255 / 7);
        int yellow = color(255, 255, 0, alpha);
            int red = color(255, 0, 0, alpha);
            int green = color(0, 255, 0, alpha);

            // Style markers based on earthquake magnitude
            if (mag < THRESHOLD_LIGHT){
                marker.setColor(green);
            }
            if (mag >= THRESHOLD_LIGHT && mag < THRESHOLD_MODERATE){
                marker.setColor(yellow);
            }
            if (mag >= THRESHOLD_MODERATE){
                marker.setColor(red);
            }

            //set radius of marker based on quake magnitude
            float radius = (float) (mag * 3.5);
            marker.setStrokeColor(color(50,15));
            marker.setRadius(radius);

        return marker;
    }

    public void draw() {
        map.draw();
        addKey();
    }
1

There are 1 answers

4
George Profenza On BEST ANSWER

The easiest option would be use an outdated version Processing that Unfolding still supports like Unfolding 0.9.6 (for Processing 2.2.1).

Since you're using eclipse you can actually compile a newer version might work for you. (I'm noticing there are a few minor updates from last year, but no release). To update via eclipse you can:

  1. get the repo (if you're on a Unix system (Linux/OSX) you might already have git installed: git clone https://github.com/tillnagel/unfolding : this way you can always pull the latest updates and rebuild easily, otherwise download zip, unzip, etc.)
  2. import the project in eclipse (via Import Existing Project)
  3. Drag build.xml from the project into the Ant View and select a target to compile. main will compile all of them including the Processing wrapper.

If you want to use command line only:

  1. Install Ant (if that's not already on your system) and add it to your PATH environment variable (e.g. to check echo $PATH on Unix or echo %PATH% on Windows and ensure the folder where ant lives is listed, if not added it)
  2. clone/download the repo
  3. run ant (e.g. ant unfolding_processing to build the Processing wrapper only or simply ant (which defaults to the main target building everything)

I can confirm manually compiling the latest version from github works with Processing 3.4 centre of earthquakes in Romania

I've uploaded the Processing wrapper here: Hopefully you can drag the .jar file on top of your eclipse project.