Mapsforge 讀取地圖

From OpenStreetMap Wiki
Jump to navigation Jump to search

返回 開發 (正體中文)

讀取大直橋附近的圖資

mapsforge 官方的地圖檔可能會省略掉一些欄位,造成輸出圖磚的不理想,這程式用來診斷地圖檔可能存在的問題。

程式

import java.io.File;
import java.util.List;

import org.mapsforge.core.model.Tag;
import org.mapsforge.core.model.Tile;
import org.mapsforge.core.util.MercatorProjection;
import org.mapsforge.map.datastore.MapDataStore;
import org.mapsforge.map.datastore.MapReadResult;
import org.mapsforge.map.datastore.PointOfInterest;
import org.mapsforge.map.datastore.Way;
import org.mapsforge.map.reader.MapFile;

public class Main {

	public static void main(String[] args) {
		String home = System.getProperty("user.home");
		MapDataStore mapDataStore = new MapFile(new File(home, "Downloads/taiwan-201512.map"));
		
		System.out.println("地圖範圍:");
		System.out.println(mapDataStore.boundingBox());
		
		double lat = 25.076;
		double lng = 121.544;
		byte   z   = 16;
		int    ty  = MercatorProjection.latitudeToTileY(lat, z);
		int    tx  = MercatorProjection.longitudeToTileX(lng, z);
		Tile   t   = new Tile(tx, ty, z, 256);
		
		System.out.println("搜尋圖資:");
		MapReadResult r = mapDataStore.readMapData(t);
		
		int i = 0;
		System.out.printf("線面: %s\n", r.ways.size());
		for (Way w : r.ways) {
			System.out.format("%02d: %s\n", i, tagsToString(w.tags));
			i++;
		}
		
		i = 0;
		System.out.printf("點: %s\n", r.pointOfInterests.size());
		for (PointOfInterest p : r.pointOfInterests) {
			System.out.format("%02d: %s\n", i, tagsToString(p.tags));
			i++;
		}
		
		mapDataStore.close();
	}
	
	private static String tagsToString(List<Tag> tags) {
		StringBuffer sbuf = new StringBuffer();
		
		for (Tag tag : tags) {
			String tagstr = String.format("%s=%s", tag.key, tag.value);
			sbuf.append(tagstr);
			sbuf.append(" ");
		}
		
		return sbuf.toString().trim();
	}

}

執行結果

地圖範圍:
minLatitude=20.627996, minLongitude=118.150901, maxLatitude=26.703049, maxLongitude=123.031181
搜尋圖資:
線面: 99
00: highway=residential name=北安路518巷
01: highway=residential name=北安路476巷
02: oneway=yes highway=secondary_link
03: boundary=administrative
04: bridge=yes oneway=yes highway=secondary name=大直橋
05: highway=residential
06: oneway=yes highway=residential name=北安路458巷
07: highway=residential name=北安路501巷
08: oneway=yes highway=secondary name=Bei'an Road
09: oneway=yes highway=secondary
10: highway=residential
11: highway=residential
12: leisure=park name=大佳河濱公園
13: natural=nosea
14: highway=residential name=明水路475巷
15: highway=residential name=北安路458巷41弄
16: boundary=administrative
17: waterway=riverbank
18: boundary=administrative
19: highway=residential name=大直街2巷
20: oneway=no highway=residential name=Dazhi Street
21: boundary=administrative
22: boundary=administrative
23: natural=water
24: leisure=park name=永安公園
25: oneway=yes highway=secondary name=Bei'an Road
26: sport=basketball leisure=pitch
27: sport=basketball leisure=pitch
28: sport=tennis leisure=pitch
29: sport=basketball leisure=pitch
30: sport=volleyball leisure=pitch
31: sport=volleyball leisure=pitch
32: oneway=yes highway=secondary name=Bei'an Road
33: leisure=track
34: leisure=track
35: sport=tennis leisure=pitch
36: area=yes natural=sea
37: sport=basketball leisure=pitch
38: waterway=river name=Keelung River
39: oneway=yes highway=secondary name=Mingshui Road
40: landuse=grass
41: sport=volleyball leisure=pitch
42: waterway=riverbank
43: waterway=riverbank
44: bridge=yes oneway=yes highway=secondary name=大直橋
45: oneway=yes highway=secondary
46: landuse=military barrier=wall name=國防部博愛營區
47: oneway=yes highway=secondary
48: oneway=yes highway=secondary name=Mingshui Road
49: sport=volleyball leisure=pitch
50: highway=residential
51: highway=residential
52: highway=residential
53: waterway=riverbank
54: highway=cycleway tracktype=grade1 name=Keelung River Right Bank Cycleway
55: bridge=yes highway=cycleway tracktype=grade1 name=Keelung River Right Bank Cycleway
56: highway=cycleway name=Keelung River Left Bank Cycleway
57: highway=cycleway tracktype=grade1 name=Keelung River Right Bank Cycleway
58: bridge=yes highway=cycleway tracktype=grade1 name=Keelung River Right Bank Cycleway
59: highway=cycleway tracktype=grade1 name=Keelung River Right Bank Cycleway
60: highway=cycleway
61: bridge=yes highway=footway
62: bridge=yes highway=footway
63: highway=footway
64: highway=footway
65: amenity=school barrier=wall name=Dazhi High School
66: highway=service
67: highway=service
68: highway=service
69: highway=service
70: amenity=school barrier=wall name=Dazhi Elementary School
71: highway=service name=北安路551巷
72: highway=service name=北安路559巷
73: amenity=school name=Bei An Junior High School
74: highway=service
75: building=yes
76: building=yes
77: building=yes
78: building=school
79: building=school
80: building=yes
81: building=yes
82: building=school
83: building=school
84: building=school
85: building=school
86: building=school
87: building=school
88: building=school
89: building=yes
90: building=school
91: building=office name=Straits Exchange Fundation
92: building=school
93: tunnel=yes railway=subway name=MRT Wenhu Line
94: oneway=yes tunnel=yes railway=subway name=MRT Wenhu Line
95: oneway=yes tunnel=yes railway=subway name=MRT Wenhu Line
96: oneway=yes tunnel=yes railway=subway name=MRT Wenhu Line
97: tunnel=yes railway=subway name=MRT Wenhu Line
98: barrier=wall
點: 2
00: place=suburb name=Dazhi
01: amenity=police name=大直派出所