Spring Boot中GraphHopper对接PostgreSQL的配置问题求助
Configuring GraphHopper with PostgreSQL in Spring Boot for Location Search
Key Issues in Your Current Configuration
Your setup uses incorrect property keys for PostgreSQL data access, and GraphHopper doesn’t natively support importing raw OSM data directly from PostgreSQL. Here’s how to resolve this:
1. Correct GraphHopper PostgreSQL Setup
Add Required Dependencies
Include these in your pom.xml (Maven) or equivalent for Gradle:
<dependency> <groupId>org.graphhopper</groupId> <artifactId>graphhopper-core</artifactId> <version>5.0</version> <!-- Use latest compatible version --> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.6.0</version> <!-- Latest stable version --> </dependency>
Fixed Spring Boot Configuration
Replace your existing config with this version (uses valid GraphHopper property names):
@Configuration public class GraphHopperConfiguration { @Value("${graphhopper.graph.dir}") private String graphDir; @Value("${db.host}") private String dbHost; @Value("${db.port}") private String dbPort; @Value("${db.name}") private String dbName; @Value("${db.user}") private String dbUser; @Value("${db.password}") private String dbPassword; @Value("${osm.pbf.path}") private String osmPbfPath; @Bean public GraphHopper graphHopper() { GraphHopper graphHopper = new GraphHopperOSM().forServer(); CmdArgs args = new CmdArgs() .put("graph.location", graphDir) // Enable PostgreSQL for graph storage .put("graph.dataaccess", "postgresql") .put("db.host", dbHost) .put("db.port", dbPort) .put("db.name", dbName) .put("db.user", dbUser) .put("db.password", dbPassword) // Path to your OSM PBF file (required for GraphHopper import) .put("datareader.file", osmPbfPath); graphHopper.init(args); // Import OSM data into PostgreSQL or load existing graph graphHopper.importOrLoad(); return graphHopper; } }
2. OSM Data in PostgreSQL: Critical Note
GraphHopper cannot import raw OSM data directly from PostgreSQL. If you’ve already loaded OSM data into PostgreSQL (e.g., via osm2pgsql), you need to:
- Export the data to an OSM PBF file using tools like Osmosis or pg_dump with spatial filters.
- Use this PBF file as the
datareader.filesource for GraphHopper’s import process.
3. Implementing Location Search
For street name or place search, choose one of these approaches:
- GraphHopper Geocoding Module: Add the
graphhopper-geocodingdependency and configure it to use a local Nominatim instance (which relies on PostgreSQL) or GraphHopper’s built-in index. - Custom PostgreSQL Queries: Write direct queries against your OSM PostgreSQL tables (e.g.,
planet_osm_points,planet_osm_roads) using PostgreSQL’s spatial functions to filter and rank search results.
内容的提问来源于stack exchange,提问作者Arabboy Ismoilov
相关产品推荐
相关产品推荐

