Custom cropping and generation of OSM map layers, however, the graphHopper program encountered an exception when parsing the map

Dear sir/miss,
To reduce memory consumption when loading map layers, I want to customize and crop the OpenStreetMap map, such as extracting only the railway network, deleting other layers such as buildings and roads, and saving only the railway network layer as an osm.pbf format file. I started from The website downloaded a shapefile compressed package in the format of. After decompression, many layer files were obtained, such as GIS_ Osm_ Water_ A_ Free_ 1. SHP, GIS_ Osm_ Roads_ Free_ 1. SHP, GIS_ Osm_ Traffic_ Free_ 1. SHP, GIS_ Osm_ Buildings_ A_ Free_ 1. SHP and others, I only extract GIS_ Osm_ Roads_ Free_ 1. SHP layer data can be opened and displayed normally through ArcGIS and JOMS software. I load GIS through JOSM_ Osm_ Roads_ Free_ 1. Successfully saved as GIS after shp_ Osm_ Roads_ Free_ 1. OSM.PBF format file. However, the graphHopper program was unable to load the map data file with the following error:

2023-09-18 14:56:22.528 [main] INFO com.graphhopper.GraphHopper - version 8.0|2023-09-04T02:43:13Z (9,21,6,5,2,9)
2023-09-18 14:56:23.173 [main] INFO com.graphhopper.GraphHopper - No custom areas are used, not given
2023-09-18 14:56:23.189 [main] INFO com.graphhopper.GraphHopper - No country rules available
2023-09-18 14:56:23.189 [main] INFO com.graphhopper.GraphHopper - start creating graph from E:\code\c\graphhopper\data\gis_osm_railways_free_1.osm.pbf
2023-09-18 14:56:23.240 [main] INFO com.graphhopper.GraphHopper - using car|RAM_STORE|2D|no_turn_cost|nodes:9,edges:21,geometry:6,location_index:5,string_index:2,nodesCH:0,shortcuts:9, memory:totalMB:252, usedMB:15
2023-09-18 14:56:23.267 [main] INFO c.g.reader.osm.WaySegmentParser - Start reading OSM file: ‘E:\code\c\graphhopper\data\gis_osm_railways_free_1.osm.pbf’
2023-09-18 14:56:23.267 [main] INFO c.g.reader.osm.WaySegmentParser - pass1 - start
2023-09-18 14:56:23.906 [main] INFO c.g.reader.osm.WaySegmentParser - pass1 - finished, processed ways: 0, accepted ways: 0, way nodes: 0, relations: 0, totalMB:252, usedMB:159
Exception in thread “main” java.lang.RuntimeException: Could not parse OSM file: E:\code\c\graphhopper\data\gis_osm_railways_free_1.osm.pbf
at com.graphhopper.reader.osm.WaySegmentParser.readOSM(
at com.graphhopper.reader.osm.WaySegmentParser.readOSM(
at com.graphhopper.reader.osm.OSMReader.readGraph(
at com.graphhopper.GraphHopper.importOSM(
at com.graphhopper.GraphHopper.process(
at com.graphhopper.GraphHopper.importOrLoad(
at com.graphhopper.example.RoutingExample.createGraphHopperInstance(
at com.graphhopper.example.RoutingExampleTest.main(
Caused by: java.lang.RuntimeException: Unable to read PBF file.
at com.graphhopper.reader.osm.pbf.PbfReader.close(
at com.graphhopper.reader.osm.OSMInputFile.close(
at com.graphhopper.reader.osm.WaySegmentParser.readOSM(
… 7 more
Caused by: java.lang.RuntimeException: A PBF decoding worker thread failed, aborting.
at com.graphhopper.reader.osm.pbf.PbfDecoder.sendResultsToSink(
at com.graphhopper.reader.osm.pbf.PbfDecoder.processBlobs(
at java.base/
Caused by: java.lang.IllegalArgumentException: Invalid OSM WAY Id: -664707; Ids must not be negative
at com.graphhopper.reader.ReaderElement.(
at com.graphhopper.reader.ReaderElement.(
at com.graphhopper.reader.ReaderWay.(
at com.graphhopper.reader.osm.pbf.PbfBlobDecoder.processWays(
at com.graphhopper.reader.osm.pbf.PbfBlobDecoder.processOsmPrimitives(
at com.graphhopper.reader.osm.pbf.PbfBlobDecoder.runAndTrapExceptions(
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(
at java.base/java.util.concurrent.ThreadPoolExecutor$
… 1 more


This issue has been bothering me for a long time, and I look forward to your help and reply.

JOSM produces negative OSM IDs which GraphHopper cannot handle. The easiest you can do is probably downloading a pbf extract from geofabrik (instead of the shapefile) and use Osmosis or Osmium to remove everything except the railway network. Or actually just try without manipulating the file at all.