【问题标题】:Why is this happening (strangeness with Processing and UnfoldingMaps)为什么会发生这种情况(Processing 和 UnfoldingMaps 的奇怪之处)
【发布时间】:2020-02-06 23:19:19
【问题描述】:

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

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

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

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

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

1.) 我使用 Processing 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();
    }

【问题讨论】:

    标签: java processing unfoldingmap


    【解决方案1】:

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

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

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

    如果你只想使用命令行:

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

    我可以确认从 github 手动编译最新版本适用于 Processing 3.4

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

    【讨论】:

    • 您好。首先,谢谢。其次,是否有大致相当于您建议使用 NetBeans 的方法?那是我选择的 IDE。另外,我是否理解处理或展开地图在某种程度上已经过时了?如果有,是哪一个?我报名的课程很老,所以我并不感到惊讶。最后,什么是蚂蚁?当我在 NetBeans 上开始一个新项目时,默认设置是“Java with Ant”,但我以前从未关心过这意味着什么……可能有太多问题需要评论。对不起。
    • 很高兴 :) 我已经很久没有使用过 NetBeans,但它是一个可靠的 Java IDE。处理库倾向于使用 eclipse,但是您可以导入 Unfolding eclipse project into Netbeans。 Ant 是a build system:简化编译、文档等任务。生成、运行等。我看到有一个详细的NetBeans Ant tutorial,但是您只需要学习如何运行 ant(因为 build.xml 已经存在)。 Unfolding Maps 的最新版本目前已过时。
    • 作为一个新的“程序员”,试图从在线存储库中提取代码,从这种情况中得到什么?我认为这将类似于了解项目的依赖关系以及这些依赖关系的版本?
    • 在一个理想的世界里,一切都应该刚刚好:Processing 非常适合新程序员,Unfolding 的 Processing 包装器应该简化更冗长的绘制 java 代码,很简单吧? :) 不幸的是,如果库/代码/文档不是最新的,您会发现自己在跳槽。外卖可能是愤世嫉俗的/积极的,这取决于你如何看待它。没有多少东西“正常工作”,很多东西都坏了,很多东西不能很好地老化并且随着时间的推移而破裂,所以在事情变得更好之前期待事情变得更糟。当事情出错时,你会学到更多(否则你甚至不会碰的东西)。
    猜你喜欢
    • 2015-07-18
    • 2021-01-24
    • 1970-01-01
    • 2018-03-06
    • 2021-04-30
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多