在 Java 中绘制经纬度坐标为 canvas 的 WFS

Draw WFS with latlong coordinates to canvas in Java

我是 Java 编程的新手,如果有任何帮助,我将不胜感激。 因此,我想在 WFS 服务器的基于 Java 的 canvas(例如 JFrame、Processing)中显示一组经纬度坐标(超过 50 个坐标)。我已经设法解析经纬度值并将其打印到控制台。现在我陷入了如何将经纬度坐标转换为屏幕坐标的问题(我想以 1000x500 的尺寸绘制它)。我试图搜索参考资料,但找不到适合像我这样的初学者的最简单的参考资料。这是我的代码的当前部分:

String[] splitc = coord.split(",");
                    String lon = splitc[0];
                    String lat = splitc[1];
                    //parse string to float
                    float loncoord=Float.parseFloat(lon);
                    float latcoord=Float.parseFloat(lat);
  1. 我可以像 https://docs.geotools.org/latest/userguide/library/referencing/axis.html 那样使用 Geotools 库的 world2screen.translate 将坐标从 WFS 传输到屏幕坐标吗?
  2. 在处理中,有一个 map() 函数 (https://processing.org/reference/map_.html) 可以从一个范围转移到另一个范围。我试过了,但它在我的 IDE.
  3. 上不起作用
  4. 一个超级菜鸟的问题,我正在尝试将 WFS 连接存储在一个函数中以便我可以在另一个 class 中调用它,我应该将它存储在 static void 中还是使用 "return"?

如果有人可以提供类似任务的示例,那将非常有帮助。谢谢(萨拉)

  1. 您可以改用这个公式:

    float x = ((WIDTH/360.0) * (180 + loncoord));
    float y = ((HEIGHT/180.0) * (90 - latcoord));
    
  2. 它应该可以工作...请注意它 returns a float 并采用以下形式的 5 个参数: map(input, inputMin, inputMax, outputMin, outputMax)

  3. 您只想创建一次连接,因此您有两个可行的选择:将连接定义为静态 class 的静态变量,或将连接定义为遵循 singleton 模式的 class 的实例变量。

    假设您选择了前一种方法,returns 连接变量的方法因此应该是 static 而不是 void:

    public static connectionType getConnection() {
        return connectionObject;
    }
    

    ... 其中 connectionType 是连接的数据类型。

最简单的方法是创建 GeoTools WFSDataStore,如果用户仅向服务端点提供 URL,此代码将构建 getCapabilities 字符串,并在需要时处理身份验证。数据存储存储在 class:

的字段中
  public FetchWFS(String url, String user, String passwd) throws IOException, URISyntaxException {
    if (!user.isEmpty()) {
      auth = true;
    }
    baseURL = new URL(url);
    List<NameValuePair> nvp = URLEncodedUtils.parse(baseURL.toURI(), "UTF-8");
    NameValuePair service = new BasicNameValuePair("service", "wfs");
    NameValuePair request = new BasicNameValuePair("request", "getCapabilities");
    NameValuePair version = new BasicNameValuePair("version", "2.0.0");
    HashMap<String, NameValuePair> parts = new HashMap<>();
    parts.put(service.getName(), service);
    parts.put(request.getName(), request);
    parts.put(version.getName(), version);
    for (NameValuePair part : nvp) {

      if (part.getName().equalsIgnoreCase("SERVICE")) {
        // We don't care what they think this should be
      } else if (part.getName().equalsIgnoreCase("REQUEST")) {
        // This must be getCapabuilities so we ignore them
      } else if (part.getName().equalsIgnoreCase("VERSION")) {
        System.out.println("Changing version to " + part.getValue());
        parts.put(version.getName(), part);
      } else {
        parts.put(part.getName(), part);
      }
    }

    URIBuilder builder = new URIBuilder();
    builder.setScheme(baseURL.getProtocol());
    builder.setHost(baseURL.getHost());
    builder.setPort(baseURL.getPort());
    builder.setPath(baseURL.getPath());
    List<NameValuePair> p = new ArrayList<>();
    p.addAll(parts.values());
    builder.setParameters(p);
    // builder.addParameter("viewparams", "q:\"mySolrQuery\"");
    URI uri = builder.build();
    System.out.println(uri);
    baseURL = uri.toURL();
    // fetch the DataStore
    Map<String, Object> params = new HashMap<>();
    params.put(WFSDataStoreFactory.URL.key, baseURL);
    // params.put(WFSDataStoreFactory.WFS_STRATEGY.key, "mapserver");
    if (auth) {
      params.put(WFSDataStoreFactory.USERNAME.key, user);
      params.put(WFSDataStoreFactory.PASSWORD.key, passwd);
    }
    // params.put(WFSDataStoreFactory.WFS_STRATEGY.key, "mapserver");
    datastore = DataStoreFinder.getDataStore(params);
  }

一旦你有了一个数据存储(任何类型),你就可以得到一个可用的列表 featureTypes,然后将其中一个(或多个)添加到地图中:

        JMapFrame frame = new JMapFrame();
        MapContent map = new MapContent();
        String[] names = datastore.getNames();
        featureSource = store.getFeatureSource(names[0]); //fetch first featureType
        schema = featureSource.getSchema();

        Style style = SLD.createSimpleStyle(schema);
        this.layer = new FeatureLayer(featureSource, style);

        map.addLayer(layer);
        frame.enableToolBar(true);
        frame.setMinimumSize(new Dimension(800, 400));
        frame.setVisible(true);

当您需要的不仅仅是简单的黑白默认地图时,GeoTools Quickstart Tutorial will help you get started with simple mapping, and the Map Styling tutorial 将允许您生成更漂亮的地图。