ReferencedEnvelope bounds = viewportModel.getBounds();
CoordinateReferenceSystem mapCrs = viewportModel.getCRS();
CoordinateReferenceSystem imageCrs = DefaultGeographicCRS.WGS84;
try {
MathTransform transform = CRS.findMathTransform(mapCrs, imageCrs);
Envelope targetEnv = JTS.transform(bounds, transform);
double west = targetEnv.getMinX();
double north = targetEnv.getMaxY();
double east = targetEnv.getMaxX();
double south = targetEnv.getMinY();
if (west < -180) {
west = -180;
}
if (west > 180) {
west = 180;
}
if (north < -90) {
north = -90;
}
if (north > 90) {
north = 90;
}
if (east < -180) {
east = -180;
}
if (east > 180) {
east = 180;
}
if (south < -90) {
south = -90;
}
if (south > 90) {
south = 90;
}
west = 180.0 + west;
north = 90.0 + north;
east = 180.0 + east;
south = 90.0 + south;
double width = east - west;
double height = north - south;
if (width < 1) {
width = 1;
}
if (height < 1) {
height = 1;
}
int x = (int) ((double) canvasBounds.width * west / 360.0);
int y = (int) ((double) h * north / 180.0);
int fw = (int) ((double) canvasBounds.width * width / 360.0);
if (fw <= 1)
fw = 2;
int fh = (int) ((double) h * height / 180.0);
if (fh <= 1)
fh = 2;
int newy = h - y;
e.gc.setForeground(color);
e.gc.setBackground(color);
e.gc.setAlpha(80);
e.gc.fillRectangle(x, newy, fw, fh);
e.gc.drawLine(x + fw / 2, 0, x + fw / 2, newy);
// e.gc.drawLine(x + fw / 2, newy + fh, x + fw / 2, h);
//
e.gc.drawLine(0, newy + fh / 2, x, newy + fh / 2);
e.gc.drawLine(x + fw, newy + fh / 2, canvasBounds.width, newy + fh / 2);
} catch (FactoryException e1) {
e1.printStackTrace();
} catch (TransformException e1) {
e1.printStackTrace();
}
}
});
// Label fillLabel = new Label(parent, SWT.NONE);
// fillLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Group geonamesGroup = new Group(parent, SWT.NONE);
geonamesGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
geonamesGroup.setLayout(new GridLayout(4, false));
geonamesGroup.setText("Geonames");
Label availableLabel = new Label(geonamesGroup, SWT.NONE);
availableLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
availableLabel.setText("Available geonames data");
URL folderUrl = Platform.getBundle(JGrassToolsPlugin.PLUGIN_ID).getResource("/geonamesfiles");
String folderPath = null;
try {
folderPath = FileLocator.toFileURL(folderUrl).getPath();
folderFile = new File(folderPath);
String[] namesArray = loadGeonamesFiles();
countriesCombo = new Combo(geonamesGroup, SWT.DROP_DOWN | SWT.READ_ONLY);
countriesCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
countriesCombo.setItems(namesArray);
countriesCombo.select(0);
countriesCombo.addSelectionListener(new SelectionAdapter(){
public void widgetSelected( SelectionEvent e ) {
int selectionIndex = countriesCombo.getSelectionIndex();
String item = countriesCombo.getItem(selectionIndex);
final String file = folderFile.getAbsolutePath() + File.separator + item + ".txt";
IRunnableWithProgress operation = new IRunnableWithProgress(){
public void run( IProgressMonitor pm ) throws InvocationTargetException, InterruptedException {
try {
populatePlacesMap(placesMap, file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
};
PlatformGIS.runInProgressDialog("Loading geonames data...", true, operation, true);
}
});
Button addNewButton = new Button(geonamesGroup, SWT.PUSH);
addNewButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
addNewButton.setText("+");
addNewButton.setToolTipText("Add a new geonames file (get it at http://download.geonames.org/export/dump/)");
addNewButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected( SelectionEvent e ) {
FileDialog fileDialog = new FileDialog(countriesCombo.getShell(), SWT.OPEN);
fileDialog.setFilterExtensions(new String[]{"*.txt"});
String newFilePath = fileDialog.open();
try {
if (newFilePath != null) {
File newFile = new File(newFilePath);
if (newFile.exists()) {
File copiedFile = new File(folderFile, newFile.getName());
FileUtils.copyFile(newFile, copiedFile);
}
}
String[] geonamesFiles = loadGeonamesFiles();
if (geonamesFiles.length > 0) {
countriesCombo.setItems(geonamesFiles);
countriesCombo.select(0);
} else {
countriesCombo.setItems(new String[]{" -- "});
}
} catch (IOException e1) {
String message = "An error occurred while copying the new geonames file into the application.";
ExceptionDetailsDialog.openError(null, message, IStatus.ERROR, JGrassToolsPlugin.PLUGIN_ID, e1);
e1.printStackTrace();
}
}
});
Button removeButton = new Button(geonamesGroup, SWT.PUSH);
removeButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
removeButton.setText("-");
removeButton.setToolTipText("Remove a geonames file");
removeButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected( SelectionEvent e ) {
int selectionIndex = countriesCombo.getSelectionIndex();
String item = countriesCombo.getItem(selectionIndex);
try {
File namesFile = new File(folderFile, item + ".txt");
if (namesFile.exists()) {
FileUtils.forceDelete(namesFile);
}
String[] geonamesFiles = loadGeonamesFiles();
if (geonamesFiles.length > 0) {
countriesCombo.setItems(geonamesFiles);
countriesCombo.select(0);
} else {
countriesCombo.setItems(new String[]{" -- "});
}
} catch (IOException e1) {
String message = "An error occurred while removing the old geonames file into the application.";
ExceptionDetailsDialog.openError(null, message, IStatus.ERROR, JGrassToolsPlugin.PLUGIN_ID, e1);
e1.printStackTrace();
}
}
});
Group placesGroup = new Group(geonamesGroup, SWT.NONE);
GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, false);
layoutData.horizontalSpan = 4;
placesGroup.setLayoutData(layoutData);
placesGroup.setLayout(new GridLayout(2, false));
placesGroup.setText("places");
placesMap = new HashMap<String, Coordinate>(1000);
populatePlacesMap(placesMap, first.getAbsolutePath());
keySet = placesMap.keySet();
final Text placesText = new Text(placesGroup, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
placesText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
placesText.setText("");
Button searchButton = new Button(placesGroup, SWT.PUSH);
searchButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
searchButton.setText("search");
searchButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected( SelectionEvent e ) {
String text = placesText.getText();
if (text.length() < 3) {
MessageBox msgBox = new MessageBox(placesText.getShell(), SWT.ICON_WARNING);
msgBox.setMessage("At least 3 letters are needed to do a search.");
msgBox.open();
return;
}
List<String> matchedList = new ArrayList<String>();
for( String name : keySet ) {
if (name.toLowerCase().matches(".*" + text.toLowerCase() + ".*")) {
matchedList.add(name);
}
}
String[] matchedArray = (String[]) matchedList.toArray(new String[matchedList.size()]);
Arrays.sort(matchedArray);
placesCombo.setItems(matchedArray);
placesCombo.select(0);
}
});
placesCombo = new Combo(placesGroup, SWT.DROP_DOWN);
placesCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
placesCombo.setItems(new String[]{ENTER_SEARCH_STRING});
Button goButton = new Button(placesGroup, SWT.PUSH);
goButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
goButton.setText("go");
goButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected( SelectionEvent e ) {
IMap map = ApplicationGIS.getActiveMap();
if (map.getMapLayers().size() < 1) {
return;
}
ReferencedEnvelope bounds = map.getViewportModel().getBounds();
CoordinateReferenceSystem mapCrs = map.getViewportModel().getCRS();
try {
GeometryFactory gF = new GeometryFactory();
CoordinateReferenceSystem placeCrs = CRS.decode("EPSG:4326");
// transform coordinates before check
MathTransform transform = CRS.findMathTransform(placeCrs, mapCrs, true);
// jts geometry
int selectionIndex = placesCombo.getSelectionIndex();
String item = placesCombo.getItem(selectionIndex);
Coordinate coordinate = placesMap.get(item);
Point pt = gF.createPoint(coordinate);