I am new to GIS and need to achieve the following:

Find the nearest train station or bus stop of given GPS coordinates in a Java application. Since there are many coordinates to be checked, an offline calculation is preferred. Visual rendering of maps is NOT required.

After some research, I thought of the following:

  1. use OpenStreetMap, download the maps
  2. extract the relevant nodes using osmosis ("... --tf accept-nodes ...")
  3. import the data into a PostgreSQL/PostGIS database
  4. finally run the queries to select nearby train station

Can you tell me if the outlined approach is possible and useful? Are there other, better ways to solve the problem?

How can I implement step 4 in a simple yet effective way? Are there Java libraries which assist with the PostGIS stuff?

  • 1
    Are you interested in the closest by linear distance, or do you need to do things along the road network?
    – scw
    Commented May 21, 2011 at 3:04
  • I need to find out if there are train stations nearby (which means within some defined radius) and which one (if any) is the actual closest.
    – Michael C.
    Commented May 21, 2011 at 4:17
  • Ok, but how do you define "closest"? Do you mean as the crow flies or actual walking/driving distance along a road network?
    – underdark
    Commented May 21, 2011 at 20:33
  • @underdark: I haven't really decided yet which one of the alternatives suits my requirements better. What kind of differences does either choice make implementation-wise?
    – Michael C.
    Commented May 22, 2011 at 6:58

1 Answer 1


A good resource for just getting point features is GeoNames, which lists even some fairly obscure train stations, although I'm not sure about bus stops. You can download the data as a CSV which makes importing into PostgreSQL easy, and you won't have to download vast amounts of OSM data only to reject most of it. Of course, if you do need road data, then OSM would be the best way to go.

On the Java side, there is the PostgreSQL JDBC driver, which will enable you to easily query the database.

In all then, I would say your approach is perfectly acceptable. PGRouting will give you the network analysis you need if you want it, and standard PostGIS functions can be used to do a simple radius check. Your Java side will be minimal, presumably just generating query strings and sending them off to a server somewhere.

If this is for a commercial project and you use OSM, check that by using a "substantial" amount of data isn't in breach of the license. GeoNames probably has a similar restriction.

  • Thanks for your answer. Are there any Java libraries which enable me to query PostGIS (e.g. for the radius check) without having to write raw SQL queries?
    – Michael C.
    Commented May 22, 2011 at 7:01
  • No, the only way of accessing the data in a PostGIS database is through SQL. But doing so isn't hard. I can recommend the book "PostGIS in Action" for details, but a simple query I put together to find the stations within 5km radius of my house using the geonames database is: SELECT "name" FROM geoname WHERE fcode = 'RSTN' AND ST_DWithin(ST_GeogFromText('SRID=4326;POINT(-2.951683 53.381924)'), geog, 5000); The only thing you'd need to do in Java is to build that string, replacing the coordinates with whatever your GPS returns. Commented May 22, 2011 at 13:40
  • 1
    GeoTools provides pure java access to PostGIS tables
    – Ian Turton
    Commented May 23, 2011 at 17:42
  • 1
    @iant: You are absolutely right, my head was in "low-level" mode: GeoTools wraps JDBC so therefore you access the database with SQL! In my defence (and to try to regain some dignity), using GeoTools for Michael's query might be more hassle than using SQL directly in this case. Commented May 23, 2011 at 17:59
  • @MerseyViking - depends on how often he wants to do it. PS Are you a Widnes Fan?
    – Ian Turton
    Commented May 23, 2011 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.