--- /dev/null
+package osm.jp.kiban25000;\r
+\r
+import javax.xml.parsers.*;\r
+import org.w3c.dom.*;\r
+import org.xml.sax.*;\r
+import java.io.*;\r
+import java.util.Iterator;\r
+import java.util.TreeMap;\r
+\r
+/**\r
+ * ====基盤地図情報のダウンロード====\r
+ *\r
+ * まず最初に、基盤地図情報をダウンロードします。\r
+ *\r
+ * 国土地理院の基盤地図情報ダウンロードページから、ログイン画面へ進みログインして、「基盤地図情報 縮尺レベル25000 JPGIS形式」を選択し、対象の地域のファイルをダウンロードします。(「JPGIS(GML)形式」ではありません)\r
+ * http://fgd.gsi.go.jp/download/\r
+ *\r
+ * 対象地域と項目の選択方法は、ダウンロードページの指示に従ってください。\r
+ *\r
+ * 「基盤地図情報項目」では「行政区画の境界線及び代表点」を選択してください。\r
+ * 「FG-JPS-39-05-Z001.zip」というような「FG-JPS-xx-05-Z001.zip」という形式のZIPファイルがダウンロードされます。\r
+ *\r
+ * ダウンロードした圧縮ファイルを解凍しておきます。\r
+ * 解凍すると「FG-JPS-44209-CommBdry-20091109-0001.xml」というような「FG-JPS-xxxx-CommBdry-xxxxxxxx-xxxx.xml」という形式のXMLファイルが出来上がります。\r
+ * @author yuu\r
+ *\r
+ */\r
+public class ConvAdmPt25000 {\r
+ File inputFile;\r
+ String admCodeStr = "";\r
+ int iCounter = 0;\r
+ boolean link = true;\r
+ TreeMap<String, AdmPt> admMap = null;\r
+\r
+ /**\r
+ * メイン\r
+ *\r
+ * <GI>\r
+ * <dataset>\r
+ *\r
+ * <AdmPt id="K6_1" uuid="fgoid:10-00150-11-1002-1530">\r
+ <fid>fgoid:10-00150-11-1002-1530</fid>\r
+ <lfSpanFr>\r
+ <jps:position>\r
+ <jps:date8601>2008-03-31</jps:date8601>\r
+ </jps:position>\r
+ </lfSpanFr>\r
+ <lfSpanTo>\r
+ <jps:position>\r
+ <jps:date8601>2999-12-31</jps:date8601>\r
+ </jps:position>\r
+ </lfSpanTo>\r
+\r
+ <devDate>\r
+ <jps:position>\r
+ <jps:date8601>2011-01-31</jps:date8601>\r
+ </jps:position>\r
+ </devDate>\r
+\r
+ <orgGILvl>25000</orgGILvl>\r
+ <vis>表示</vis>\r
+\r
+ <pos id="K6_2" uuid="fgoid:10-00150-11-1002-1530-g">\r
+ <jps:CRS uuidref="fguuid:jgd2000.bl"/>\r
+ <jps:position>\r
+ <jps:coordinate>37.055674 139.338021</jps:coordinate>\r
+ </jps:position>\r
+ </pos>\r
+\r
+ <type>その他</type>\r
+ <name>福島県南会津郡檜枝岐村</name>\r
+ <admCode>07364</admCode>\r
+\r
+ </AdmPt>\r
+\r
+ </dataset>\r
+\r
+ </GI>\r
+\r
+ */\r
+ public static void main(String[] argv)\r
+ {\r
+ boolean link = true;\r
+ for (String arg : argv) {\r
+ if (arg.equals("-nolink")) {\r
+ link = false;\r
+ }\r
+ }\r
+\r
+ File dir = new File(".");\r
+ File[] files = dir.listFiles();\r
+ int counter = 0;\r
+ for (File iFile : files) {\r
+ if (checkFile(iFile)) {\r
+ counter++;\r
+ ConvAdmPt25000 t = new ConvAdmPt25000(iFile, link);\r
+ t.runner();\r
+ }\r
+ }\r
+ System.out.println("["+ counter +"]つのファイルを処理しました。");\r
+ }\r
+\r
+ public ConvAdmPt25000 (File iFile, boolean link) {\r
+ this.inputFile = iFile;\r
+ this.link = link;\r
+ }\r
+\r
+ public void runner() {\r
+ DocumentBuilderFactory factory;\r
+ DocumentBuilder builder;\r
+ Node root;\r
+\r
+ this.iCounter = 0;\r
+ String iStr = this.inputFile.getName();\r
+ String[] nameAry = iStr.split("-");\r
+ String fileName = "";\r
+ for (int i=0; i < nameAry.length; i++) {\r
+ if (i != 0) {\r
+ fileName = fileName + "-";\r
+ }\r
+ fileName = fileName + nameAry[i];\r
+ if (i == 2) {\r
+ this.admCodeStr = nameAry[2];\r
+ }\r
+ }\r
+\r
+ File outputFile = new File(iStr.substring(0, iStr.length() - 4) +".gpx");\r
+ BufferedWriter wGpx = null;\r
+\r
+ File htmlFile = new File(iStr.substring(0, iStr.length() - 4) +".html");\r
+ BufferedWriter wHtml = null;\r
+\r
+ File wikiFile = new File(iStr.substring(0, iStr.length() - 4) +".txt");\r
+ BufferedWriter wWiki = null;\r
+\r
+ File csvFile = new File(iStr.substring(0, iStr.length() - 4) +".csv");\r
+ BufferedWriter wCsv = null;\r
+\r
+ try {\r
+ wGpx = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));\r
+ wHtml = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8"));\r
+ wWiki = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(wikiFile), "UTF-8"));\r
+ wCsv = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8"));\r
+\r
+ wGpx.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");\r
+ wGpx.newLine();\r
+ wGpx.write("<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" version=\"1.1\" creator=\"osmtracker-android\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd \">");\r
+ wGpx.newLine();\r
+\r
+ wWiki.write("=== "+ admCodeStr +": "+ getCodeName(Integer.parseInt(admCodeStr)) +" ===");\r
+ wWiki.newLine();\r
+ wWiki.write("{| class=\"wikitable sortable\"");\r
+ wWiki.newLine();\r
+ wWiki.write("|- ");\r
+ wWiki.newLine();\r
+ wWiki.write("! style=\"width:70px\" | admCode");\r
+ wWiki.newLine();\r
+ wWiki.write("! | name");\r
+ wWiki.newLine();\r
+ wWiki.write("! style=\"width:70px\" | type");\r
+ wWiki.newLine();\r
+ wWiki.write("! | 備考/最終更新日");\r
+ wWiki.newLine();\r
+\r
+ wCsv.write("\"都道府県コード\",\"都道府県名\",\"admCode\",\"Name\",\"type\",\"LAT\",\"LON\",\"備考/最終更新日\"");\r
+ wCsv.newLine();\r
+\r
+ wHtml.write("<!DOCTYPE html>");\r
+ wHtml.newLine();\r
+ wHtml.write("<html><head><meta charset=\"utf-8\" /></head>");\r
+ wHtml.newLine();\r
+ wHtml.write("<body><table border='1'>");\r
+ wHtml.newLine();\r
+ wHtml.write("<tr>");\r
+ wHtml.write("<td>admCode</td>");\r
+ wHtml.write("<td>name</td>");\r
+ wHtml.write("<td></td>");\r
+ wHtml.write("</tr>");\r
+ wHtml.newLine();\r
+\r
+ factory = DocumentBuilderFactory.newInstance();\r
+ builder = factory.newDocumentBuilder();\r
+ factory.setIgnoringElementContentWhitespace(true);\r
+ factory.setIgnoringComments(true);\r
+ factory.setValidating(true);\r
+ root = builder.parse(inputFile.getName());\r
+\r
+ //---------------------------------------\r
+ admMap = new TreeMap<String, AdmPt>();\r
+ showNodes(root, "");\r
+ //---------------------------------------\r
+\r
+ Iterator<String> it = admMap.keySet().iterator();\r
+ while (it.hasNext()) {\r
+ String code = (String) it.next();\r
+ AdmPt adm = admMap.get(code);\r
+\r
+ wWiki.write("|- ");\r
+ wWiki.newLine();\r
+ wWiki.write("| " + adm.code);\r
+ wWiki.newLine();\r
+ wWiki.write("| " + adm.name);\r
+ wWiki.newLine();\r
+ wWiki.write("| " + adm.type);\r
+ wWiki.newLine();\r
+ wWiki.write("| ");\r
+ wWiki.newLine();\r
+ \r
+ wCsv.write("\""+ admCodeStr +"\",\""+ getCodeName(Integer.parseInt(admCodeStr)) +"\",");\r
+ wCsv.write("\""+ adm.code +"\",\""+ adm.name +"\",\""+ adm.type +"\",");\r
+ wCsv.write("\""+ adm.latStr +"\",\""+ adm.lonStr +"\",\""+ adm.dateStr +"\"");\r
+ wCsv.newLine();\r
+\r
+ wHtml.write("<tr>");\r
+ wHtml.write("<td>"+ adm.code +"</td>");\r
+ wHtml.write("<td>"+ adm.name +"</td>");\r
+ wHtml.write("<td>"+ adm.type +"</td>");\r
+ wHtml.write("</tr>");\r
+ wHtml.newLine();\r
+\r
+ wGpx.write("<wpt lat=\""+ adm.latStr +"\" lon=\""+ adm.lonStr +"\">");\r
+ wGpx.newLine();\r
+ wGpx.write(" <time>"+ adm.dateStr +"T00:00:00Z</time>");\r
+ wGpx.newLine();\r
+ wGpx.write(" <name><![CDATA["+ adm.name +"]]></name>");\r
+ wGpx.newLine();\r
+ wGpx.write("</wpt>");\r
+ wGpx.newLine();\r
+ }\r
+\r
+ wGpx.write("</gpx>");\r
+ wGpx.newLine();\r
+\r
+ wHtml.write("</table></body></html>");\r
+ wHtml.newLine();\r
+\r
+ wWiki.write("|}");\r
+ wWiki.newLine();\r
+\r
+ } catch (ParserConfigurationException e0) {\r
+ System.out.println(e0.getMessage());\r
+ } catch (SAXException e1){\r
+ System.out.println(e1.getMessage());\r
+ } catch (IOException e2) {\r
+ System.out.println(e2.getMessage());\r
+ }\r
+ finally {\r
+ if (wGpx != null) {\r
+ try {\r
+ wGpx.close();\r
+ wHtml.close();\r
+ }\r
+ catch (IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ if (wWiki != null) {\r
+ try {\r
+ wWiki.close();\r
+ }\r
+ catch (IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ if (wCsv != null) {\r
+ try {\r
+ wCsv.close();\r
+ }\r
+ catch (IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ public void showNodes(Node node, String space) throws IOException {\r
+ NodeList nodes = node.getChildNodes();\r
+ for (int i=0; i<nodes.getLength(); i++) {\r
+ Node node2 = nodes.item(i);\r
+ if (node2.getNodeName().equals("AdmPt")) {\r
+ showAdmPt(node2);\r
+ }\r
+ else {\r
+ showNodes(node2, space + " ");\r
+ }\r
+ }\r
+ }\r
+\r
+ public void showAdmPt(Node node) throws IOException {\r
+ String positionStr = "";\r
+ String typeStr = "";\r
+ String nameStr = "";\r
+ String latStr = "";\r
+ String lonStr = "";\r
+ String dateStr = "";\r
+ String codeStr = "";\r
+\r
+ NodeList nodes = node.getChildNodes();\r
+ for (int i=0; i < nodes.getLength(); i++) {\r
+ Node node2 = nodes.item(i);\r
+ if (node2.getNodeName().equals("admCode")) {\r
+ codeStr = node2.getTextContent();\r
+ }\r
+ else if (node2.getNodeName().equals("pos")) {\r
+ NodeList nodes3 = node2.getChildNodes();\r
+ for (int j=0; j < nodes3.getLength(); j++) {\r
+ Node node3 = nodes3.item(j);\r
+ if (node3.getNodeName().equals("jps:position")) {\r
+ NodeList nodes4 = node3.getChildNodes();\r
+ for (int k=0; k < nodes4.getLength(); k++) {\r
+ Node node4 = nodes4.item(k);\r
+ if (node4.getNodeName().equals("jps:coordinate")) {\r
+ positionStr = node4.getTextContent();\r
+ String[] str4Ary = positionStr.split(" ");\r
+ latStr = str4Ary[0];\r
+ lonStr = str4Ary[1];\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ else if (node2.getNodeName().equals("devDate")) {\r
+ NodeList nodes3 = node2.getChildNodes();\r
+ for (int j=0; j < nodes3.getLength(); j++) {\r
+ Node node3 = nodes3.item(j);\r
+ if (node3.getNodeName().equals("jps:position")) {\r
+ NodeList nodes4 = node3.getChildNodes();\r
+ for (int k=0; k < nodes4.getLength(); k++) {\r
+ Node node4 = nodes4.item(k);\r
+ if (node4.getNodeName().equals("jps:date8601")) {\r
+ dateStr = node4.getTextContent();\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ else if (node2.getNodeName().equals("type")) {\r
+ typeStr = node2.getTextContent();\r
+\r
+ }\r
+ else if (node2.getNodeName().equals("name")) {\r
+ nameStr = node2.getTextContent();\r
+ }\r
+ }\r
+\r
+ if (admMap.get(codeStr) == null) {\r
+ AdmPt adm = new AdmPt();\r
+ adm.code = codeStr;\r
+ adm.name = nameStr;\r
+ adm.type = typeStr;\r
+ adm.latStr = latStr;\r
+ adm.lonStr = lonStr;\r
+ adm.dateStr = dateStr;\r
+ admMap.put(codeStr, adm);\r
+ }\r
+ }\r
+\r
+ class AdmPt {\r
+ public String code;\r
+ public String name;\r
+ public String type;\r
+ public String latStr;\r
+ public String lonStr;\r
+ public String dateStr;\r
+ }\r
+\r
+ /**\r
+ *\r
+ * 例 [FG-JPS-07-AdmPt25000-20110131-0001.xml]\r
+ *\r
+ * @param f\r
+ * @return\r
+ */\r
+ static boolean checkFile(File f) {\r
+ String name = f.getName();\r
+ if (!name.startsWith("FG-JPS-")) {\r
+ return false;\r
+ }\r
+ if (!name.toUpperCase().endsWith(".XML")) {\r
+ return false;\r
+ }\r
+ if (name.toUpperCase().indexOf("-ADMPT25000-") < 0) {\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+ public static String getCodeName(int code) {\r
+ switch (code) {\r
+ case 1:\r
+ return "北海道";\r
+ case 2:\r
+ return "青森県";\r
+ case 3:\r
+ return "岩手県";\r
+ case 4:\r
+ return "宮城県";\r
+ case 5:\r
+ return "秋田県";\r
+ case 6:\r
+ return "山形県";\r
+ case 7:\r
+ return "福島県";\r
+ case 8:\r
+ return "茨城県";\r
+ case 9:\r
+ return "栃木県";\r
+ case 10:\r
+ return "群馬県";\r
+ case 11:\r
+ return "埼玉県";\r
+ case 12:\r
+ return "千葉県";\r
+ case 13:\r
+ return "東京都";\r
+ case 14:\r
+ return "神奈川県";\r
+ case 15:\r
+ return "新潟県";\r
+ case 16:\r
+ return "富山県";\r
+ case 17:\r
+ return "石川県";\r
+ case 18:\r
+ return "福井県";\r
+ case 19:\r
+ return "山梨県";\r
+ case 20:\r
+ return "長野県";\r
+ case 21:\r
+ return "岐阜県";\r
+ case 22:\r
+ return "静岡県";\r
+ case 23:\r
+ return "愛知県";\r
+ case 24:\r
+ return "三重県";\r
+ case 25:\r
+ return "滋賀県";\r
+ case 26:\r
+ return "京都府";\r
+ case 27:\r
+ return "大阪府";\r
+ case 28:\r
+ return "兵庫県";\r
+ case 29:\r
+ return "奈良県";\r
+ case 30:\r
+ return "和歌山";\r
+ case 31:\r
+ return "鳥取県";\r
+ case 32:\r
+ return "島根県";\r
+ case 33:\r
+ return "岡山県";\r
+ case 34:\r
+ return "広島県";\r
+ case 35:\r
+ return "山口県";\r
+ case 36:\r
+ return "徳島県";\r
+ case 37:\r
+ return "香川県";\r
+ case 38:\r
+ return "愛媛県";\r
+ case 39:\r
+ return "高知県";\r
+ case 40:\r
+ return "福岡県";\r
+ case 41:\r
+ return "佐賀県";\r
+ case 42:\r
+ return "長崎県";\r
+ case 43:\r
+ return "熊本県";\r
+ case 44:\r
+ return "大分県";\r
+ case 45:\r
+ return "宮崎県";\r
+ case 46:\r
+ return "鹿児島";\r
+ case 47:\r
+ return "沖縄県";\r
+ }\r
+ return "";\r
+ }\r
+\r
+}
\ No newline at end of file