2

Can geotools use shapefiles as boundaries (or exclusion areas) for the mouse's cursor ?

My goal is to draw on a map a trajectory for a vehicle. The user drags the mouse on the map to draw the trajectory (of a car for example). But I don't want the car to go in a river, or a lake or even cross an ocean.

I have found shapefiles representing the continents but I don't know how to tell geotools to not let the mouse step over a shape line while drawing a trajectory.

I am not asking for the code but just an orientation on which tool(s) would allow me to do this, or even if there is something in geotools to do it.

More basically, when the user is pressing the mouse's left button, how can I forbid the mouse to cross a line (or a shape border) ?

1 Answers1

0

GeoTools can help you with this but you will need to write (quite a lot of) code to handle it.

GeoTools has no issue reading your shapefiles and displaying them on the screen usually using swing or swt.

Depending on how you are displaying the map you'll need some sort of MouseEvent handler that can check if it is in the shape (again GeoTools can help here as it has tools to help you convert the mouse position to real world coordinates and check if those coordinates are on land etc). Then if the check fails you can "prevent" the mouse moving in that direction. To do this quickly you will certainly need to make sure the features have a spatial index (which can be done with Shapefiles but is better in a database like PostGIS).

Ian Turton
  • 10,018
  • 1
  • 28
  • 47
  • Thanks ! Right now I don't have the time to try this. But I will probably do it this week end. This helped me a lot and at least I am now sure that it is possible. Also, when you say "prevent the mouse moving in that direction", do you mean using Robot to replace the mouse at the point where it "exited" the shape ? Or is there a cleaner way to do this ? – user3745720 Aug 27 '15 at 07:30
  • I would allow the mouse to keep moving but keep the icon that represents the car where the edge of the lake is, since you can control that – Ian Turton Aug 27 '15 at 08:57