在地图上显示连接点的线 - 接收空地图

时间:2017-07-20 12:20:56

标签: java gis geotools

我想绘制一条连接地图上2个点的线。

我正在使用的代码:

public class Quickstart {


    public static void main(String[] args) throws Exception {
        // display a data store file chooser dialog for shapefiles
        File file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return;
        }

      FileDataStore store = FileDataStoreFinder.getDataStore(file);
      SimpleFeatureSource featureSource = store.getFeatureSource();

      GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();

      // ask for current and destination positions
      double latitude, longitude, latitudeDest, longitudeDest;
      Scanner reader = new Scanner(System.in);
      reader.useLocale(Locale.US);
      System.out.println("Enter reference longitude and latitude:\n");
      longitude = reader.nextDouble();
      latitude = reader.nextDouble();
      System.out.println("Enter destination longitude and latitude:\n");
      longitudeDest = reader.nextDouble();
      latitudeDest = reader.nextDouble();
      reader.close();

        final String EPSG4326 = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\"," +
                "\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\", " + 
                "0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]";

        CoordinateReferenceSystem crs = CRS.parseWKT(EPSG4326);        


        Point start = gf.createPoint(new Coordinate(longitude, latitude));
        Point end = gf.createPoint(new Coordinate(longitudeDest, latitudeDest));

        GeodeticCalculator gc = new GeodeticCalculator(crs);
        gc.setStartingPosition(JTS.toDirectPosition(start.getCoordinate(), crs));
        gc.setDestinationPosition(JTS.toDirectPosition(end.getCoordinate(), crs));

        // Calculate distance between points
        double distance = gc.getOrthodromicDistance();

        int totalmeters = (int) distance;
        int km = totalmeters / 1000;
        int meters = totalmeters - (km * 1000);
        float remaining_cm = (float) (distance - totalmeters) * 10000;
        remaining_cm = Math.round(remaining_cm);
        float cm = remaining_cm / 100;

        System.out.println("Distance = " + km + "km " + meters + "m " + cm + "cm");

        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
        builder.setName("TwoDistancesType");
        builder.setCRS(DefaultGeographicCRS.WGS84);
        builder.add("location", Point.class);

        // build the type
        final SimpleFeatureType TYPE = builder.buildFeatureType();

        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
        featureBuilder.add(start);
        //featureBuilder.add(end);

        SimpleFeature feature = featureBuilder.buildFeature(null);
        DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal", TYPE);
        featureCollection.add(feature);

        Style style = SLD.createSimpleStyle(TYPE, Color.red);
        Layer layer = new FeatureLayer(featureCollection, style);

        // Create a map content and add our shapefile to it
        MapContent map = new MapContent();
        map.setTitle("TEST");

        map.addLayer(layer);

        // Now display the map
        JMapFrame.showMap(map);


    }

}

我有两个问题:

1)我无法向featureBuilder添加第二个功能。它不允许它。它显示Can handle 1 attributes only, index is 1

那么,我怎么画一条线?

2)使用上面的代码,我收到了:

org.geotools.renderer.lite.StreamingRenderer fireErrorEvent SEVERE: The scale denominator must be positive
java.lang.IllegalArgumentException: The scale denominator must be positive

-------更新------------------------

在@Michael提出第一个问题的解决方案之后,现在我不再收到有关分母的错误,但我收到一张空地图(空格)。

-----根据@iant建议更新----------------

所以,我尝试了这个。修复了一个坐标,它保存了点的坐标(开始和结束),然后用这些坐标创建了一个线串,并将它添加到了featurebuilder。

        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
        builder.setName("TwoDistancesType");
        builder.setCRS(DefaultGeographicCRS.WGS84);

        builder.add("line", LineString.class); //added a linestring class


        final SimpleFeatureType TYPE = builder.buildFeatureType();

        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);

        Coordinate[] coordinates = {start.getCoordinate(), end.getCoordinate()};       
        LineString line = gf.createLineString(coordinates);
        featureBuilder.add(line);

即使我正在加载地图(countries.shp),它也会显示一个带有红线的空白空间。

------解决方案-------------

好的,所以解决方案是(感谢@iants评论):

样式样式= SLD.createLineStyle(Color.red,2.0f);     Layer layer = new FeatureLayer(featureCollection,style);

// Create style for the file
Style shpStyle = SLD.createSimpleStyle(TYPE, Color.blue);
Layer shpLayer = new FeatureLayer(featureSource, shpStyle);

// Create a map content and add our shapefile to it
MapContent map = new MapContent();
map.setTitle("TEST");
map.addLayer(layer);
map.addLayer(shpLayer);

现在你在蓝色地图上有一条红线!

2 个答案:

答案 0 :(得分:2)

免责声明:我从未使用过geotools。

查看the source code of SimpleFeatureBuilderadd调用set会在以下情况下引发该错误:

if(index >= values.length)
    throw new ArrayIndexOutOfBoundsException("Can handle "
    + values.length + " attributes only, index is " + index);

values填写在此处:

values = new Object[featureType.getAttributeCount()];

所以很明显这个问题是因为你的Type只有一个属性。改变它有两个:

SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("TwoDistancesType");
builder.setCRS(DefaultGeographicCRS.WGS84);
builder.add("start", Point.class);
builder.add("end",   Point.class);

答案 1 :(得分:2)

您需要从您的点创建一个LineString,然后将其存储在您的功能中。

然后您应该获得正确的比例,但您可能希望首先将一些其他数据(例如海岸线)添加到地图中。快速入门教程可以向您展示如何做到这一点。