リビジョン f8402ef5
| learn/java/javafx/HelloMap/src/hellomap/HelloMapViewController.java | ||
|---|---|---|
|
import javafx.scene.paint.Color;
|
||
|
import javafx.scene.transform.Affine;
|
||
|
import javafx.stage.FileChooser;
|
||
|
import javafx.util.converter.NumberStringConverter;
|
||
|
import org.nocrala.tools.gis.data.esri.shapefile.exception.InvalidShapeFileException;
|
||
|
|
||
|
/**
|
||
| ... | ... | |
|
private static final Logger logger = Logger.getLogger(HelloMapViewController.class.getName());
|
||
|
private static final double DOT_PITCH_METER = 0.247 / 1_000;
|
||
|
private static final Affine IDENTITY_TRANSFORM = new Affine();
|
||
|
private double SCALE_RATE = 1.4;
|
||
|
private static final double SCALE_RATE = 1.4;
|
||
|
|
||
|
@FXML
|
||
|
private Canvas map;
|
||
| ... | ... | |
|
private Label scaleLabel;
|
||
|
@FXML
|
||
|
private AnchorPane rootPane;
|
||
|
|
||
|
private Affine transform = new Affine();
|
||
|
|
||
|
private List<MapPolyline> polylines = new ArrayList<>();
|
||
|
|
||
|
private DoubleProperty scaleProperty = new SimpleDoubleProperty(1);
|
||
|
|
||
|
private Point2D dragStartPoint;
|
||
|
private Point2D translateAtDragStart;
|
||
|
private Point2D translate = new Point2D(0f, 0f);
|
||
|
|
||
|
/**
|
||
|
* シェープファイルをロードする。
|
||
|
* <p>
|
||
|
* 現時点ではポリラインにのみ対応。
|
||
|
*
|
||
|
* @param event
|
||
|
*/
|
||
| ... | ... | |
|
private void handleMapLoadAction(ActionEvent event) {
|
||
|
FileChooser chooser = new FileChooser();
|
||
|
chooser.setTitle("Select ESRI Shapefile");
|
||
|
chooser.setInitialDirectory(Paths.get(System.getProperty("user.home"), "Documents", "mapdata").toFile());
|
||
|
chooser.setInitialDirectory(Paths.get(System.getProperty("user.dir"), "mapdata").toFile());
|
||
|
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Shapefile", "*.shp"));
|
||
|
File selected = chooser.showOpenDialog(map.getScene().getWindow());
|
||
|
if (selected != null) {
|
||
| ... | ... | |
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 画面の初期化。
|
||
|
* <p>
|
||
|
* <ul>
|
||
|
* <li>縮尺の初期値、縮尺表示ラベルの連動設定
|
||
|
* <li>Canvasをウィンドウサイズの変更に追従させる設定
|
||
|
* <li>地図投影座標系から表示座標系へのAffine初期値設定
|
||
|
* <li>スクロール(マウスホイール)操作で地図拡大縮小する設定
|
||
|
* <li>マウスドラッグ操作で地図移動する設定
|
||
|
* </ul>
|
||
|
*
|
||
|
* @param url
|
||
|
* @param rb
|
||
|
*/
|
||
|
@Override
|
||
|
public void initialize(URL url, ResourceBundle rb) {
|
||
|
scaleLabel.textProperty().bindBidirectional(scaleProperty, new NumberStringConverter());
|
||
|
scaleProperty.addListener((target, oldValue, newValue) -> {
|
||
|
scaleLabel.setText(String.format("Scale: 1/%,d", (int) (1 / (newValue.doubleValue() * DOT_PITCH_METER))));
|
||
|
});
|
||
|
scaleProperty.set(1 / mapToScale(10_000_000));
|
||
|
|
||
|
map.widthProperty().bind(rootPane.widthProperty().subtract(128));
|
||
|
map.heightProperty().bind(rootPane.heightProperty());
|
||
|
map.widthProperty().addListener(ev -> drawMap());
|
||
| ... | ... | |
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 地図描画メソッド。
|
||
|
*/
|
||
|
private void drawMap() {
|
||
|
logger.info(() -> String.format("transform=%s", transform));
|
||
|
|
||
| ... | ... | |
|
gc.setTransform(transform);
|
||
|
gc.setStroke(Color.BROWN);
|
||
|
gc.setLineWidth(1); // / scaleProperty.get());
|
||
|
|
||
|
for (MapPolyline shape : polylines) {
|
||
|
gc.strokePolyline(shape.getxPoints(), shape.getyPoints(), shape.getNumPoints());
|
||
|
}
|
||
|
polylines.stream().forEach(shape
|
||
|
-> gc.strokePolyline(shape.getxPoints(), shape.getyPoints(), shape.getNumPoints())
|
||
|
);
|
||
|
logger.info("after draw");
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 地図クリアメソッド。
|
||
|
*/
|
||
|
private void clearMap() {
|
||
|
GraphicsContext gc = map.getGraphicsContext2D();
|
||
|
gc.setTransform(IDENTITY_TRANSFORM);
|
||
地図の縮尺表示を地図っぽくし、初期縮尺を小縮尺に、また、地図色味調整した。