2017-07-21 13 views
2

私はマップ上にユーザが提供する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(); 

    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"); 

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

    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); 
    builder.setName("TwoDistancesType"); 
    builder.setCRS(DefaultGeographicCRS.WGS84); 
    //builder.add("start", Point.class); 
    //builder.add("end", Point.class); 
    builder.add("line", LineString.class); 
    // build the type 
    final SimpleFeatureType TYPE = builder.buildFeatureType(); 

    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE); 
    featureBuilder.add(line); 

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

    // Create style for the line 
    //Style style = SLD.createSimpleStyle(TYPE, Color.red); 
    Style style = 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); 

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


} 

私は2つの質問があります。

1)は、どのように私はラインの方向を示すことができ、開始点から終了しますか?

2)プログラムを実行して地図を表示するときは、線ストリング(赤線)を手動で検索してから地図をズームインして見つける必要があります。座標)が表示されますか?あなたはSLDようなものが必要なスタイルのために

答えて

1

はなるコードで、hereを説明:ラインにズーム

// Create style for the line 
    // Style style = SLD.createSimpleStyle(TYPE, Color.red); 
    org.geotools.styling.Style style = SLD.createLineStyle(Color.red, 2.0f); 
    StyleBuilder sb = new StyleBuilder(); 
    FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(); 
    PointSymbolizer point = sb.createPointSymbolizer(); 
    Mark mark = sb.createMark("shape://oarrow"); 
    mark.setFill(sb.createFill(Color.RED)); 
    mark.setStroke(sb.createStroke(Color.red)); 

    Graphic graphic = sb.createGraphic(null, mark, null); 
    graphic.setRotation(ff.function("endAngle", ff.property("line"))); 
    point.setGraphic(graphic); 

    point.setGeometry(ff.function("endpoint", ff.property("line"))); 

    Rule rule = sb.createRule(point); 
    style.getFeatureTypeStyles()[0].addRule(rule); 
    Layer layer = new FeatureLayer(featureCollection, style); 

は、ラインの境界にマップビューポートを設定する場合だけです。

MapViewport viewport = new MapViewport(featureCollection.getBounds()); 
    map.setViewport(viewport); 

周囲を見ることができるように、これらの境界を少しずつ(10%?)拡大したいと思う場合は、

enter image description here

あなたが使用することができますStyleBuilderで非推奨メソッド避けるためにEDIT

:バウンディングボックスを拡大

style.featureTypeStyles().get(0).rules().add(rule); 

は封筒にある程度の距離を追加するだけの場合です:

ReferencedEnvelope bounds = featureCollection.getBounds(); 
    double delta = bounds.getWidth()/20.0; //5% on each side 
    bounds.expandBy(delta); 
    MapViewport viewport = new MapViewport(bounds); 
    map.setViewport(viewport); 
+0

:ありがとうございました!まだテストはしていませんが、問題が発生した場合はお知らせします。ありがとうございます(上書きされました) – George

+0

:[その他の質問について]私は、 (ports.shpを使用して)最も近いポート(https://stackoverflow.com/questions/45189672/read-coordinates-from-shp-file-and-compute-distance)、私が書いたコードをチェックできると思いますか?あなたが私にthisと一緒に別の投稿を作成してもらいたいかどうか教えてください。ありがとうございました! – George

+0

さて、それはうまくいく!ちょうど2つのもの。 1)あなたは私が周囲を取得するために境界を少し拡大することができると言う。私は '新しいMapViewport(featureCollection.getBounds()+0.1);)を試したが、うまくいかない。別の方法ですか? 'style.getFeatureTypeStyles()[0] .addRule(rule);'は償却されています。 – George

関連する問題