首页 > 解决方案 > 为什么会发生这种情况(Processing 和 UnfoldingMaps 的奇怪之处)

问题描述

作为 coursera UCSD Java 课程的一部分,我们正在使用处理和展开地图库。

该课程为您提供了我正在尝试扩展的入门代码。但是我遇到了一个问题。

在我的工作计算机上,我大部分时间都在其中学习,我启动了一个非常好的应用程序。我决定把它带回家给我妻子看,把我的 github repo 克隆到我的家用笔记本电脑上。

没有包含任何库/JAR 文件,所以我直接从 Processing and Unfolding 的下载页面下载了它们。结果是一场噩梦。一个又一个的错误,一个又一个的错误。我假设 coursera/UCSD 使用了旧版本的库,而新版本不向后兼容。看起来很糟糕,但无论如何。

接下来,我从 coursera 视线中下载了所有的 JAR 文件。结果稍微好一点,但事情仍然很古怪。也就是说,当我创建一个UnfoldingMap()对象时,窗口内的位置参数和大小绝对没有任何作用。

1.)我使用处理size()方法来放大或缩小包含窗口,它会放大或缩小地图对象。它完全忽略了UnfoldingMap().

2.) 地图对象总是被推到窗口的左下角,不管它的位置参数。

3.) 我选择的灰色background()确实显示在地图周围,但背景本身被一个大的黑色区域包围,它也随size()参数缩放。

这是一些代码:

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();
    }

标签: javaprocessingunfoldingmap

解决方案


最简单的选择是使用 Unfolding 仍然支持的过时版本 Processing,例如Unfolding 0.9.6 (for Processing 2.2.1)

由于您使用的是 eclipse,因此您实际上可以编译一个较新的版本可能对您有用。(我注意到去年有一些小更新,但没有发布)。要通过 Eclipse 更新,您可以:

  1. 获取 repo(如果您使用的是 Unix 系统(Linux/OSX),您可能已经git安装了::git clone https://github.com/tillnagel/unfolding这样您就可以随时提取最新更新并轻松重建,否则下载 zip、解压缩等)
  2. 在eclipse中导入项目(通过导入现有项目)
  3. 将 build.xml 从项目拖到Ant 视图中并选择一个目标进行编译。main将编译所有这些,包括处理包装器。

如果您只想使用命令行:

  1. 安装Ant(如果您的系统上尚未安装 Ant)并将其添加到您的PATH环境变量中(例如,echo $PATH在 Unix 或echo %PATH%Windows 上检查并确保列出了 ant 所在的文件夹,如果未添加它)
  2. 克隆/下载 repo
  3. 运行 ant(例如ant unfolding_processing,仅构建处理包装器或简单地构建ant(默认为main构建所有内容的目标)

我可以确认从 github 手动编译最新版本适用于 Processing 3.4 罗马尼亚地震中心

我在这里上传了处理包装器:希望您可以将 .jar 文件拖到您的 Eclipse 项目之上。


推荐阅读