Created
September 16, 2012 23:05
-
-
Save upsilon/3734773 to your computer and use it in GitHub Desktop.
国土数値情報(行政区域データ) OpenStreetMapインポート用スクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| # | |
| # ksj2osm-admin.pl | |
| # | |
| # Modified by upsilon <kim.upsilon@bucyou.net> | |
| # | |
| # Original Author: Tatata (http://wiki.openstreetmap.org/wiki/User:Tatata) | |
| # | |
| # This source based on http://wiki.openstreetmap.org/wiki/User:Tatata/ksj2osm-admin.pl | |
| # is licenced under Creative Commons Attribution-ShareAlike 2.0 license. | |
| use strict; | |
| use warnings; | |
| use encoding "utf8"; | |
| #use encoding "utf8", STDOUT => "shiftjis", STDERR => "shiftjis"; # for Windows | |
| use Encode; | |
| use open IO => "utf8"; | |
| use XML::Parser; | |
| ##### | |
| # | |
| # KSJ2 Administrative area | |
| # | |
| # National-Land Numerical Information (Administrative area) 2010, MLIT Japan | |
| # 国土数値情報(行政区域データ)平成22年 国土交通省 | |
| # | |
| # Files | |
| # Input | |
| # XML file : N03-110331_*.xml | |
| # Output | |
| # Osm file : ksj2osm-admin-place-0.osm (places) | |
| # Osm file : ksj2osm-admin-inner-*.osm (municipality boundary) | |
| # Osm file : ksj2osm-admin-outer-*.osm (prefecture boundary w/coastline) | |
| # Log file : ksj2osm-admin.log | |
| # | |
| ##### | |
| our $switch_place = 1; # 0 : no output, 1 : output ksj2osm-admin-place-0.osm | |
| our $switch_inner = 1; # 0 : no output, 1 : output ksj2osm-admin-inner-*.osm | |
| our $switch_outer = 1; # 0 : no output, 1 : output ksj2osm-admin-outer-*.osm | |
| our $max_ways_inner = 27; # for splitting output inner files. (27 for Chiba inner) | |
| our $max_ways_outer = 90; # for splitting output outer files. (90 for Chiba outer) | |
| our $file_in = "N03-110331_14"; | |
| our $file_name = "ksj2osm-admin"; | |
| our $east = 160.00000000; | |
| our $west = 110.00000000; | |
| our $south = 10.00000000; | |
| our $north = 60.00000000; | |
| our %centerHash; | |
| our $count_place_files = 0; | |
| our $count_inner_files = 0; | |
| our $count_outer_files = 0; | |
| our $count_ways = 0; | |
| our %curveHash; | |
| our $negative_id = 0; | |
| our $node_ref; | |
| our %nodes = (); | |
| our $num_error = 0; | |
| our $num_nodes = 0; | |
| our $num_places = 0; | |
| our $num_ways = 0; | |
| our %ocHash; | |
| our %placeHash; | |
| our %surfaceHash; | |
| our @workArray; | |
| our %workHash; | |
| our $workString; | |
| # our $x = 0; #####DEBUG | |
| sub main() { | |
| my $parser = new XML::Parser(ErrorContext => 3, | |
| Handlers => {Init => \&handle_init, | |
| Start => \&handle_start, | |
| Char => \&handle_char, | |
| End => \&handle_end, | |
| Final => \&handle_final}); | |
| $parser->parsefile("$file_in.xml"); | |
| } | |
| sub handle_init() { | |
| open_log(); | |
| } | |
| sub handle_start() { | |
| my ($expat, $element, %hash) = @_; | |
| if ($element eq "ksj:EC01" | |
| || $element eq "ksj:ARE" | |
| || $element eq "jps:GM_Surface" | |
| || $element eq "jps:GM_CompositeCurve.generator" | |
| || $element eq "jps:GM_OrientableCurve" | |
| || $element eq "jps:GM_OrientablePrimitive.primitive" | |
| || $element eq "jps:GM_Curve" | |
| || $element eq "GM_PointRef.point" | |
| || $element eq "jps:GM_Point") {get_id(@_);} | |
| $workString = ""; # add 2009-05-26 | |
| } | |
| sub handle_char () { | |
| my ($expat, $string) = @_; | |
| # $workString = $string; | |
| # $workString .= ""; # mod 2009-05-26 | |
| $workString .= $string; # mod 2009-10-16 | |
| } | |
| sub handle_end() { | |
| my ($expat, $element) = @_; | |
| if ($element eq "ksj:PRN" | |
| || $element eq "ksj:SUN" | |
| || $element eq "ksj:CON" | |
| || $element eq "ksj:CN2" | |
| || $element eq "ksj:AAC" | |
| || $element eq "DirectPosition.coordinate") {get_element(@_);} | |
| elsif ($element eq "ksj:EC01") {add_place();} | |
| elsif ($element eq "jps:GM_SurfaceBoundary.exterior") {add_exterior();} | |
| elsif ($element eq "jps:GM_SurfaceBoundary.interior") {add_interior();} | |
| elsif ($element eq "jps:GM_Surface") {add_surface();} | |
| elsif ($element eq "jps:GM_OrientableCurve") {add_oc();} | |
| elsif ($element eq "jps:GM_Curve") {add_curve();} | |
| elsif ($element eq "jps:GM_Point") {replace_point();} | |
| } | |
| sub handle_final() { | |
| update_hash(); | |
| my $time = localtime(time); | |
| print LOG "***** End of processing $file_in.xml : $time\n"; | |
| print "***** End of processing $file_in.xml : $time\n"; | |
| print LOG "*****\n"; | |
| print LOG "***** Dump of placeHash\n"; | |
| foreach my $item(keys %placeHash) { | |
| print LOG "* 1st key: $item\n"; | |
| foreach my $item2(keys %{$placeHash{$item}}) { | |
| print LOG "** 2nd key: $item2\n"; | |
| foreach my $item3(keys %{$placeHash{$item}{$item2}}) { | |
| print LOG "*** 3rd key: $item3\n"; | |
| foreach my $item4(keys %{$placeHash{$item}{$item2}{$item3}}) { | |
| print LOG "**** 4th key: $item4\n"; | |
| foreach my $item5(keys %{$placeHash{$item}{$item2}{$item3}{$item4}}) { | |
| print LOG "***** 5th key: $item5, value: $placeHash{$item}{$item2}{$item3}{$item4}{$item5}\n"; | |
| } | |
| } | |
| } | |
| } | |
| print LOG "*****\n"; | |
| } | |
| print LOG "***** Dump of centerHash\n"; | |
| foreach my $item(keys %centerHash) { | |
| print LOG "* 1st key: $item\n"; | |
| foreach my $item2(keys %{$centerHash{$item}}) { | |
| print LOG "** 2nd key: $item2, value: $centerHash{$item}{$item2}\n"; | |
| } | |
| } | |
| print LOG "*****\n"; | |
| #####DEBUG | |
| # print LOG "***** Dump of surfaceHash\n"; | |
| # foreach my $item(keys %surfaceHash) { | |
| # print LOG "* 1st key: $item\n"; | |
| # foreach my $item2(keys %{$surfaceHash{$item}}) { | |
| # print LOG "** 2nd key: $item2, value: $surfaceHash{$item}{$item2}\n"; | |
| # } | |
| # } | |
| # print LOG "*****\n"; | |
| # | |
| # print LOG "***** Dump of curveHash\n"; | |
| # foreach my $item(keys %curveHash) { | |
| # print LOG "* 1st key: $item\n"; | |
| # foreach my $item2(keys %{$curveHash{$item}}) { | |
| # print LOG "** 2nd key: $item2, value: $curveHash{$item}{$item2}\n"; | |
| # } | |
| # } | |
| # print LOG "*****\n"; | |
| ##### | |
| create_osm(); | |
| close_log(); | |
| } | |
| sub open_log() { | |
| my $time = localtime(time); | |
| open(LOG, ">$file_name.log"); | |
| print LOG "***** KSJ2 Administrative area Data 2011 : Start $time\n"; | |
| print "***** KSJ2 Administrative area 2011 : Start $time\n"; | |
| print LOG "***** Switch : place = $switch_place, inner = $switch_inner, outer = $switch_outer.\n"; | |
| print "***** Switch : place = $switch_place, inner = $switch_inner, outer = $switch_outer.\n"; | |
| print LOG "***** Max ways : inner = $max_ways_inner, outer = $max_ways_outer.\n"; | |
| print "***** Max ways : inner = $max_ways_inner, outer = $max_ways_outer.\n"; | |
| print LOG "***** Extracting from $file_in.xml\n"; | |
| print "***** Extracting from $file_in.xml\n"; | |
| } | |
| sub close_log() { | |
| my $time = localtime(time); | |
| print LOG "***** Done!: End $time\n"; | |
| close LOG; | |
| print "***** Done!: End $time\n"; | |
| } | |
| sub get_id() { | |
| my ($expat, $element, %hash) = @_; | |
| if ($element eq "ksj:ARE" ) { | |
| if (exists($hash{"idref"})) { | |
| $workHash{"ARE"} = $hash{"idref"}; | |
| } | |
| else { | |
| print LOG "* idref not found in element ksj:ARE.\n"; | |
| print "* idref not found in element ksj:ARE.\n"; | |
| while (my ($key, $value) = each(%hash)) { | |
| print LOG "key: $key , value: $value \n"; | |
| print "key: $key , value: $value \n"; | |
| } | |
| die "idref not found in element ksj:ARE."; | |
| } | |
| } | |
| elsif ($element eq "jps:GM_CompositeCurve.generator" ) { | |
| if (exists($hash{"idref"})) { | |
| if (exists($workHash{"CompositeCurve"})) { | |
| $workHash{"CompositeCurve"} .= ","; | |
| } | |
| $workHash{"CompositeCurve"} .= $hash{"idref"}; | |
| } | |
| else { | |
| print LOG "* idref not found in element jps:GM_CompositeCurve.generator.\n"; | |
| print "* idref not found in element jps:GM_CompositeCurve.generator.\n"; | |
| while (my ($key, $value) = each(%hash)) { | |
| print LOG "key: $key , value: $value \n"; | |
| print "key: $key , value: $value \n"; | |
| } | |
| die "idref not found in element jps:GM_CompositeCurve.generator."; | |
| } | |
| } | |
| elsif ($element eq "jps:GM_OrientableCurve" ) { | |
| if (exists($hash{"id"})) { | |
| $workHash{"id"} = $hash{"id"}; | |
| $workHash{"element"} = $hash{"jps:GM_OrientableCurve"}; | |
| } | |
| else { | |
| print LOG "* id not found in element jps:GM_Curve.\n"; | |
| print "* id not found in element jps:GM_Curve.\n"; | |
| while (my ($key, $value) = each(%hash)) { | |
| print LOG "key: $key , value: $value \n"; | |
| print "key: $key , value: $value \n"; | |
| } | |
| die "id not found in element jps:GM_Curve."; | |
| } | |
| } | |
| elsif ($element eq "jps:GM_OrientablePrimitive.primitive" ) { | |
| if (exists($workHash{"element"})) { | |
| if (exists($hash{"idref"})) { | |
| $workHash{"primitive"} = $hash{"idref"}; | |
| } | |
| else { | |
| print LOG "* idref not found in element jps:GM_OrientablePrimitive.primitive.\n"; | |
| print "* idref not found in element jps:GM_OrientablePrimitive.primitive.\n"; | |
| while (my ($key, $value) = each(%hash)) { | |
| print LOG "key: $key , value: $value \n"; | |
| print "key: $key , value: $value \n"; | |
| } | |
| die "idref not found in element jps:GM_OrientablePrimitive.primitive."; | |
| } | |
| } | |
| } | |
| elsif ($element eq "GM_PointRef.point") { | |
| if (exists($hash{"idref"})) { | |
| if (exists($workHash{"points"})) { | |
| $workHash{"points"} .= ","; | |
| } | |
| $workHash{"points"} .= $hash{"idref"}; | |
| } | |
| else { | |
| print LOG "* id not found in element GM_PointRef.point.\n"; | |
| print "* id not found in element GM_PointRef.point.\n"; | |
| while (my ($key, $value) = each(%hash)) { | |
| print LOG "key: $key , value: $value \n"; | |
| print "key: $key , value: $value \n"; | |
| } | |
| die "id not found in element GM_PointRef.point."; | |
| } | |
| } | |
| else { | |
| if (exists($hash{"id"})) { | |
| $workHash{"id"} = $hash{"id"}; | |
| } | |
| else { | |
| print LOG "* id not found in element $element.\n"; | |
| print "* id not found in element $element.\n"; | |
| while (my ($key, $value) = each(%hash)) { | |
| print LOG "key: $key , value: $value \n"; | |
| print "key: $key , value: $value \n"; | |
| } | |
| die "id not found in element $element"; | |
| } | |
| } | |
| } | |
| sub get_element() { | |
| my ($expat, $element) = @_; | |
| if ($element eq "ksj:PRN" ) {$workHash{"PRN"} = $workString;} | |
| elsif ($element eq "ksj:SUN" ) {$workHash{"SUN"} = $workString;} | |
| elsif ($element eq "ksj:CON" ) {$workHash{"CON"} = $workString;} | |
| elsif ($element eq "ksj:CN2" ) {$workHash{"CN2"} = $workString;} | |
| elsif ($element eq "ksj:AAC" ) {$workHash{"AAC"} = $workString;} | |
| elsif ($element eq "DirectPosition.coordinate" ) { | |
| if (exists($workHash{"points"})) { | |
| $workHash{"points"} .= ","; | |
| } | |
| $workHash{"points"} .= $workString; | |
| } | |
| $workString = ""; # add 2009-05-26 | |
| } | |
| # set tag data for writing OSM file | |
| sub write_osm_constant_tag(){ | |
| my $tags; | |
| my %hash = ( | |
| "created_by" => "National-Land-Numerical-Information_MLIT_Japan", | |
| "note:en" => "National-Land Numerical Information (Administrative area) 2011, MLIT Japan", | |
| "note:ja" => "国土数値情報(行政区域データ)平成22年 国土交通省", | |
| "source" => "KSJ2", | |
| "source_ref" => "http://nlftp.mlit.go.jp/ksj/jpgis/datalist/KsjTmplt-N03.html", | |
| "KSJ2:filename" => $file_in . ".xml", | |
| ); | |
| foreach my $key ( keys( %hash ) ){ | |
| $tags .= "<tag k=\"$key\" v=\"$hash{$key}\"/>\n"; | |
| } | |
| return $tags; | |
| } # end sub write_osm_constant_tag() | |
| sub add_place() { | |
| unless (exists($workHash{"SUN"})) {$workHash{"SUN"} = "*";} | |
| unless (exists($workHash{"CON"})) {$workHash{"CON"} = "*";} | |
| unless (exists($workHash{"CN2"})) {$workHash{"CN2"} = "*";} | |
| $placeHash{$workHash{"PRN"}}{$workHash{"SUN"}}{$workHash{"CON"}}{$workHash{"CN2"}}{"AAC"} = $workHash{"AAC"}; | |
| if (exists($placeHash{$workHash{"PRN"}}{$workHash{"SUN"}}{$workHash{"CON"}}{$workHash{"CN2"}}{"EC01"})) { | |
| $placeHash{$workHash{"PRN"}}{$workHash{"SUN"}}{$workHash{"CON"}}{$workHash{"CN2"}}{"EC01"} .= ","; | |
| } | |
| $placeHash{$workHash{"PRN"}}{$workHash{"SUN"}}{$workHash{"CON"}}{$workHash{"CN2"}}{"EC01"} .= $workHash{"id"}; | |
| if (exists($placeHash{$workHash{"PRN"}}{$workHash{"SUN"}}{$workHash{"CON"}}{$workHash{"CN2"}}{"ARE"})) { | |
| $placeHash{$workHash{"PRN"}}{$workHash{"SUN"}}{$workHash{"CON"}}{$workHash{"CN2"}}{"ARE"} .= ","; | |
| } | |
| $placeHash{$workHash{"PRN"}}{$workHash{"SUN"}}{$workHash{"CON"}}{$workHash{"CN2"}}{"ARE"} .= $workHash{"ARE"}; | |
| %workHash = (); | |
| } | |
| sub add_exterior() { | |
| if (exists($workHash{"exterior"})) { | |
| $workHash{"exterior"} .= ","; | |
| } | |
| $workHash{"exterior"} .= $workHash{"CompositeCurve"}; | |
| delete $workHash{"CompositeCurve"}; | |
| } | |
| sub add_interior() { | |
| if (exists($workHash{"interior"})) { | |
| $workHash{"interior"} .= ","; | |
| } | |
| $workHash{"interior"} .= $workHash{"CompositeCurve"}; | |
| delete $workHash{"CompositeCurve"}; | |
| } | |
| sub add_surface() { | |
| if (exists($workHash{"exterior"})) { | |
| $surfaceHash{$workHash{"id"}}{"exterior"} = $workHash{"exterior"}; | |
| } | |
| if (exists($workHash{"interior"})) { | |
| $surfaceHash{$workHash{"id"}}{"interior"} = $workHash{"interior"}; | |
| } | |
| %workHash = (); | |
| } | |
| sub add_oc() { | |
| $ocHash{$workHash{"id"}} = $workHash{"primitive"}; | |
| %workHash = (); | |
| } | |
| sub add_curve() { | |
| $curveHash{$workHash{"id"}}{"points"} = $workHash{"points"}; | |
| %workHash = (); | |
| # $x++; #####DEBUG | |
| # print "add_curve $x\n"; #####DEBUG | |
| } | |
| sub replace_point() { | |
| my $string; | |
| foreach my $item(keys %curveHash) { | |
| if (exists($curveHash{$item}{"points"})) { | |
| $curveHash{$item}{"points"} =~ s/$workHash{"id"}/$workHash{"points"}/g; | |
| } | |
| } | |
| %workHash = (); | |
| # $x++; #####DEBUG | |
| # print "replace_point $x\n"; #####DEBUG | |
| } | |
| sub update_hash() { | |
| my @surfaceArray; | |
| my @curveArray; | |
| %workHash = (); | |
| foreach my $item(keys %placeHash) { | |
| foreach my $item2(keys %{$placeHash{$item}}) { | |
| foreach my $item3(keys %{$placeHash{$item}{$item2}}) { | |
| foreach my $item4(keys %{$placeHash{$item}{$item2}{$item3}}) { | |
| @surfaceArray = split(/,/, $placeHash{$item}{$item2}{$item3}{$item4}{"ARE"}); | |
| foreach my $surface(@surfaceArray) { | |
| if (exists($surfaceHash{$surface}{"exterior"})) { | |
| @curveArray = split(/,/, $surfaceHash{$surface}{"exterior"}); | |
| foreach my $curve(@curveArray) {update_curvehash($curve);} | |
| } | |
| if (exists($surfaceHash{$surface}{"interior"})) { | |
| @curveArray = split(/,/, $surfaceHash{$surface}{"interior"}); | |
| foreach my $curve(@curveArray) {update_curvehash($curve);} | |
| } | |
| } | |
| maxmin_latlong($item, $item2, $item3, $item4); | |
| %workHash = (); | |
| } | |
| } | |
| } | |
| } | |
| update_centerhash(); | |
| # $x++; #####DEBUG | |
| # print "update_hash $x\n"; #####DEBUG | |
| } | |
| sub update_curvehash() { | |
| my ($curve) = @_; | |
| if (exists($ocHash{$curve})) {$curve = $ocHash{$curve};} | |
| @workArray = split(/,/, $curveHash{$curve}{"points"}); | |
| foreach my $coordinate(@workArray) { | |
| my ($lat, $long) = split(/\s/, $coordinate); | |
| if (defined($lat) && defined($long)) { | |
| unless (($lat eq "") || ($long eq "")) { | |
| unless (($lat > $north) || ($lat < $south) | |
| || ($long > $east) || ($long < $west)) { | |
| if (exists($workHash{"maxlat"})) { | |
| if ($lat > $workHash{"maxlat"}) {$workHash{"maxlat"} = $lat;} | |
| } else {$workHash{"maxlat"} = $lat;} | |
| if (exists($workHash{"maxlong"})) { | |
| if ($long > $workHash{"maxlong"}) {$workHash{"maxlong"} = $long;} | |
| } else {$workHash{"maxlong"} = $long;} | |
| if (exists($workHash{"minlat"})) { | |
| if ($lat < $workHash{"minlat"}) {$workHash{"minlat"} = $lat;} | |
| } else {$workHash{"minlat"} = $lat;} | |
| if (exists($workHash{"minlong"})) { | |
| if ($long < $workHash{"minlong"}) {$workHash{"minlong"} = $long;} | |
| } else {$workHash{"minlong"} = $long;} | |
| } | |
| } | |
| } | |
| } | |
| if (exists($curveHash{$curve}{"num"})) {$curveHash{$curve}{"num"}++;} | |
| else {$curveHash{$curve}{"num"} = 0;} | |
| } | |
| sub maxmin_latlong(){ | |
| my ($item, $item2, $item3, $item4) = @_; | |
| $centerHash{$item.$item2.$item3.$item4}{"maxlat"} = $workHash{"maxlat"}; | |
| $centerHash{$item.$item2.$item3.$item4}{"maxlong"} = $workHash{"maxlong"}; | |
| $centerHash{$item.$item2.$item3.$item4}{"minlat"} = $workHash{"minlat"}; | |
| $centerHash{$item.$item2.$item3.$item4}{"minlong"} = $workHash{"minlong"}; | |
| if (exists($centerHash{$item.$item2.$item3}{"maxlat"})) { | |
| if ($centerHash{$item.$item2.$item3}{"maxlat"} < $workHash{"maxlat"}) { | |
| $centerHash{$item.$item2.$item3}{"maxlat"} = $workHash{"maxlat"}; | |
| } | |
| } | |
| else { | |
| $centerHash{$item.$item2.$item3}{"maxlat"} = $workHash{"maxlat"}; | |
| } | |
| if (exists($centerHash{$item.$item2.$item3}{"maxlong"})) { | |
| if ($centerHash{$item.$item2.$item3}{"maxlong"} < $workHash{"maxlong"}) { | |
| $centerHash{$item.$item2.$item3}{"maxlong"} = $workHash{"maxlong"}; | |
| } | |
| } | |
| else { | |
| $centerHash{$item.$item2.$item3}{"maxlong"} = $workHash{"maxlong"}; | |
| } | |
| if (exists($centerHash{$item.$item2.$item3}{"minlat"})) { | |
| if ($centerHash{$item.$item2.$item3}{"minlat"} > $workHash{"minlat"}) { | |
| $centerHash{$item.$item2.$item3}{"minlat"} = $workHash{"minlat"}; | |
| } | |
| } | |
| else { | |
| $centerHash{$item.$item2.$item3}{"minlat"} = $workHash{"minlat"}; | |
| } | |
| if (exists($centerHash{$item.$item2.$item3}{"minlong"})) { | |
| if ($centerHash{$item.$item2.$item3}{"minlong"} > $workHash{"minlong"}) { | |
| $centerHash{$item.$item2.$item3}{"minlong"} = $workHash{"minlong"}; | |
| } | |
| } | |
| else { | |
| $centerHash{$item.$item2.$item3}{"minlong"} = $workHash{"minlong"}; | |
| } | |
| if (exists($centerHash{$item.$item2}{"maxlat"})) { | |
| if ($centerHash{$item.$item2}{"maxlat"} < $workHash{"maxlat"}) { | |
| $centerHash{$item.$item2}{"maxlat"} = $workHash{"maxlat"}; | |
| } | |
| } | |
| else { | |
| $centerHash{$item.$item2}{"maxlat"} = $workHash{"maxlat"}; | |
| } | |
| if (exists($centerHash{$item.$item2}{"maxlong"})) { | |
| if ($centerHash{$item.$item2}{"maxlong"} < $workHash{"maxlong"}) { | |
| $centerHash{$item.$item2}{"maxlong"} = $workHash{"maxlong"}; | |
| } | |
| } | |
| else { | |
| $centerHash{$item.$item2}{"maxlong"} = $workHash{"maxlong"}; | |
| } | |
| if (exists($centerHash{$item.$item2}{"minlat"})) { | |
| if ($centerHash{$item.$item2}{"minlat"} > $workHash{"minlat"}) { | |
| $centerHash{$item.$item2}{"minlat"} = $workHash{"minlat"}; | |
| } | |
| } | |
| else { | |
| $centerHash{$item.$item2}{"minlat"} = $workHash{"minlat"}; | |
| } | |
| if (exists($centerHash{$item.$item2}{"minlong"})) { | |
| if ($centerHash{$item.$item2}{"minlong"} > $workHash{"minlong"}) { | |
| $centerHash{$item.$item2}{"minlong"} = $workHash{"minlong"}; | |
| } | |
| } | |
| else { | |
| $centerHash{$item.$item2}{"minlong"} = $workHash{"minlong"}; | |
| } | |
| if (exists($centerHash{$item}{"maxlat"})) { | |
| if ($centerHash{$item}{"maxlat"} < $workHash{"maxlat"}) { | |
| $centerHash{$item}{"maxlat"} = $workHash{"maxlat"}; | |
| } | |
| } | |
| else { | |
| $centerHash{$item}{"maxlat"} = $workHash{"maxlat"}; | |
| } | |
| if (exists($centerHash{$item}{"maxlong"})) { | |
| if ($centerHash{$item}{"maxlong"} < $workHash{"maxlong"}) { | |
| $centerHash{$item}{"maxlong"} = $workHash{"maxlong"}; | |
| } | |
| } | |
| else { | |
| $centerHash{$item}{"maxlong"} = $workHash{"maxlong"}; | |
| } | |
| if (exists($centerHash{$item}{"minlat"})) { | |
| if ($centerHash{$item}{"minlat"} > $workHash{"minlat"}) { | |
| $centerHash{$item}{"minlat"} = $workHash{"minlat"}; | |
| } | |
| } | |
| else { | |
| $centerHash{$item}{"minlat"} = $workHash{"minlat"}; | |
| } | |
| if (exists($centerHash{$item}{"minlong"})) { | |
| if ($centerHash{$item}{"minlong"} > $workHash{"minlong"}) { | |
| $centerHash{$item}{"minlong"} = $workHash{"minlong"}; | |
| } | |
| } | |
| else { | |
| $centerHash{$item}{"minlong"} = $workHash{"minlong"}; | |
| } | |
| } | |
| sub update_centerhash(){ | |
| foreach my $item(keys %centerHash) { | |
| $centerHash{$item}{"lat"} = $centerHash{$item}{"maxlat"}; | |
| $centerHash{$item}{"long"} = $centerHash{$item}{"maxlong"}; | |
| $centerHash{$item}{"lat"} += $centerHash{$item}{"minlat"}; | |
| $centerHash{$item}{"long"} += $centerHash{$item}{"minlong"}; | |
| $centerHash{$item}{"lat"} /= 2; | |
| $centerHash{$item}{"long"} /= 2; | |
| } | |
| } | |
| sub create_osm(){ | |
| my $i = 0; | |
| my ($item, $item2, $item3, $item4) = ""; | |
| if ($switch_place == 1) { | |
| open(OSM, ">$file_name-place-$count_place_files.osm"); | |
| printf LOG "***** open %s-place-%s.osm\n", $file_name, $count_place_files; | |
| print OSM "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; | |
| print OSM "<osm version=\"0.5\" generator=\"KSJ2OSM\">\n"; | |
| foreach $item(keys %placeHash) { | |
| write_place(1, $item, $item2, $item3, $item4); | |
| foreach $item2(keys %{$placeHash{$item}}) { | |
| unless ($item2 eq "*") {write_place(2, $item, $item2, $item3, $item4);} | |
| foreach $item3(keys %{$placeHash{$item}{$item2}}) { | |
| unless ($item3 eq "*") {write_place(3, $item, $item2, $item3, $item4);} | |
| foreach $item4(keys %{$placeHash{$item}{$item2}{$item3}}) { | |
| unless ($item4 eq "*") {write_place(4, $item, $item2, $item3, $item4);} | |
| } | |
| } | |
| } | |
| } | |
| print OSM "</osm>"; | |
| close OSM; | |
| printf LOG "***** close %s-place-%s.osm\n", $file_name, $count_place_files; | |
| $count_place_files++; | |
| } | |
| if ($switch_inner == 1) { | |
| open(OSM, ">$file_name-inner-$count_inner_files.osm"); | |
| printf LOG "***** open %s-inner-%s.osm\n", $file_name, $count_inner_files; | |
| print OSM "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; | |
| print OSM "<osm version=\"0.5\" generator=\"KSJ2OSM\">\n"; | |
| foreach $item(keys %curveHash) { | |
| if ($curveHash{$item}{"num"} >= 1) { | |
| @workArray = split(/,/, $curveHash{$item}{"points"}); | |
| foreach my $coordinate(@workArray) { | |
| $i = write_node($i, $item, $coordinate); | |
| } | |
| $count_ways++; | |
| write_way("inner", $i, $item); | |
| $i = 0; | |
| if ($count_ways == $max_ways_inner) { | |
| print OSM "</osm>"; | |
| close OSM; | |
| printf LOG "***** close %s-inner-%s.osm\n", $file_name, $count_inner_files; | |
| %nodes = (); | |
| $count_ways = 0; | |
| $count_inner_files++; | |
| open(OSM, ">$file_name-inner-$count_inner_files.osm"); | |
| printf LOG "***** open %s-inner-%s.osm\n", $file_name, $count_inner_files; | |
| print OSM "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; | |
| print OSM "<osm version=\"0.5\" generator=\"KSJ2OSM\">\n"; | |
| } | |
| } | |
| } | |
| print OSM "</osm>"; | |
| close OSM; | |
| printf LOG "***** close %s-inner-%s.osm\n", $file_name, $count_inner_files; | |
| $count_inner_files++; | |
| } | |
| if ($switch_outer == 1) { | |
| open(OSM, ">$file_name-outer-$count_outer_files.osm"); | |
| printf LOG "***** open %s-outer-%s.osm\n", $file_name, $count_outer_files; | |
| print OSM "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; | |
| print OSM "<osm version=\"0.5\" generator=\"KSJ2OSM\">\n"; | |
| foreach $item(keys %curveHash) { | |
| if ($curveHash{$item}{"num"} == 0) { | |
| @workArray = split(/,/, $curveHash{$item}{"points"}); | |
| foreach my $coordinate(@workArray) { | |
| $i = write_node($i, $item, $coordinate); | |
| } | |
| $count_ways++; | |
| write_way("outer", $i, $item); | |
| $i = 0; | |
| if ($count_ways == $max_ways_outer) { | |
| print OSM "</osm>"; | |
| close OSM; | |
| printf LOG "***** close %s-outer-%s.osm\n", $file_name, $count_outer_files; | |
| %nodes = (); | |
| $count_ways = 0; | |
| $count_outer_files++; | |
| open(OSM, ">$file_name-outer-$count_outer_files.osm"); | |
| printf LOG "***** open %s-outer-%s.osm\n", $file_name, $count_outer_files; | |
| print OSM "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; | |
| print OSM "<osm version=\"0.5\" generator=\"KSJ2OSM\">\n"; | |
| } | |
| } | |
| } | |
| print OSM "</osm>"; | |
| close OSM; | |
| printf LOG "***** close %s-outer-%s.osm\n", $file_name, $count_outer_files; | |
| $count_outer_files++; | |
| } | |
| printf LOG | |
| "***** Administrative area : skipped %d error coordinates\n" | |
| , $num_error; | |
| printf LOG | |
| "***** Administrative area : %d nodes on %d ways\n" | |
| , $num_nodes, $num_ways; | |
| printf LOG | |
| "***** Administrative area : %d nodes tagged as place\n" | |
| , $num_places; | |
| printf LOG | |
| "***** Administrative area : output place files = %d\n" | |
| , $count_place_files; | |
| printf LOG | |
| "***** Administrative area : output inner files = %d\n" | |
| , $count_inner_files; | |
| printf LOG | |
| "***** Administrative area : output outer files = %d\n" | |
| , $count_outer_files; | |
| printf | |
| "***** Administrative area : skipped %d error coordinates\n" | |
| , $num_error; | |
| printf | |
| "***** Administrative area : %d nodes on %d ways\n" | |
| , $num_nodes, $num_ways; | |
| printf | |
| "***** Administrative area : %d nodes tagged as place\n" | |
| , $num_places; | |
| printf | |
| "***** Administrative area : output place files = %d\n" | |
| , $count_place_files; | |
| printf | |
| "***** Administrative area : output inner files = %d\n" | |
| , $count_inner_files; | |
| printf | |
| "***** Administrative area : output outer files = %d\n" | |
| , $count_outer_files; | |
| } | |
| sub write_place() { | |
| my ($flag, $item, $item2, $item3, $item4) = @_; | |
| my $name; | |
| my $string; | |
| my $place_tag; | |
| my $tmp_tags; | |
| $negative_id--; | |
| my $tags = &write_osm_constant_tag(); | |
| if ($flag == 1) { | |
| $name = $item; | |
| $string = $item; | |
| $tags .= "<tag k=\"KSJ2:PRN\" v=\"$item\"/>"; | |
| if ($item eq "北海道") { | |
| $tags .= "<tag k=\"is_in\" v=\"日本 (Japan)\"/>"; | |
| $tags .= "<tag k=\"is_in:country\" v=\"日本 (Japan)\"/>"; | |
| $place_tag = "state"; | |
| } | |
| else { | |
| $tags .= "<tag k=\"is_in\" v=\"日本 (Japan)\"/>"; | |
| $tags .= "<tag k=\"is_in:country\" v=\"日本 (Japan)\"/>"; | |
| $tags .= "<tag k=\"is_in:state\" v=\" ()\"/>"; | |
| $place_tag = "region"; | |
| } | |
| } | |
| elsif ($flag == 2) { | |
| $name = $item2; | |
| $string = $item.$item2; | |
| $tags .= "<tag k=\"KSJ2:PRN\" v=\"$item\"/>"; | |
| $tags .= "<tag k=\"KSJ2:SUN\" v=\"$item2\"/>"; | |
| $tags .= "<tag k=\"is_in\" v=\"$item ()\"/>"; | |
| $tags .= "<tag k=\"is_in:state\" v=\"$item ()\"/>"; | |
| $place_tag = "region"; | |
| } | |
| elsif ($flag == 3) { | |
| $name = $item3; | |
| $string = $item.$item2.$item3; | |
| $tags .= "<tag k=\"KSJ2:PRN\" v=\"$item\"/>"; | |
| unless ($item2 eq "*") { | |
| $tags .= "<tag k=\"KSJ2:SUN\" v=\"$item2\"/>"; | |
| $tags .= "<tag k=\"is_in\" v=\"$item2 ()\"/>"; | |
| $tags .= "<tag k=\"is_in:region\" v=\"$item2 ()\"/>"; | |
| } | |
| else { | |
| $tags .= "<tag k=\"is_in\" v=\"$item ()\"/>"; | |
| $tags .= "<tag k=\"is_in:region\" v=\"$item ()\"/>"; | |
| } | |
| $tags .= "<tag k=\"KSJ2:CON\" v=\"$item3\"/>"; | |
| if ($item3 =~ /郡$/) {$place_tag = "county";} | |
| else {$place_tag = "city";} # for "政令指定都市" "東京特別区" | |
| } | |
| elsif ($flag == 4) { | |
| $name = $item4; | |
| $string = $item.$item2.$item3.$item4; | |
| $tags .= "<tag k=\"KSJ2:PRN\" v=\"$item\"/>"; | |
| $tmp_tags = "<tag k=\"is_in\" v=\"$item ()\"/>"; | |
| $tmp_tags .= "<tag k=\"is_in:region\" v=\"$item ()\"/>"; | |
| unless ($item2 eq "*") { | |
| $tags .= "<tag k=\"KSJ2:SUN\" v=\"$item2\"/>"; | |
| $tmp_tags = "<tag k=\"is_in\" v=\"$item2 ()\"/>"; | |
| $tmp_tags .= "<tag k=\"is_in:region\" v=\"$item2 ()\"/>"; | |
| } | |
| unless ($item3 eq "*") { | |
| $tags .= "<tag k=\"KSJ2:CON\" v=\"$item3\"/>"; | |
| if ($item3 =~ /郡$/) { | |
| $tmp_tags = "<tag k=\"is_in\" v=\"$item3 ()\"/>"; | |
| $tmp_tags .= "<tag k=\"is_in:county\" v=\"$item3 ()\"/>"; | |
| } # for "町" "村" | |
| else { | |
| $tmp_tags = "<tag k=\"is_in\" v=\"$item3 ()\"/>"; | |
| $tmp_tags .= "<tag k=\"is_in:city\" v=\"$item3 ()\"/>"; | |
| } # for "政令指定都市の区" | |
| } | |
| $tags .= $tmp_tags; | |
| $tags .= "<tag k=\"KSJ2:CN2\" v=\"$item4\"/>"; | |
| if ($item4 =~ /市$/) {$place_tag = "city";} | |
| elsif ($item4 =~ /区$/) {$place_tag = "town";} | |
| elsif ($item4 =~ /町$/) {$place_tag = "town";} | |
| elsif ($item4 =~ /村$/) {$place_tag = "village";} | |
| else {$place_tag = "locality";} # for "所属未定地" | |
| } | |
| $tags .= "<tag k=\"place\" v=\"$place_tag\"/>"; | |
| $tags .= "<tag k=\"layer\" v=\"1\"/>"; | |
| $tags .= "<tag k=\"name\" v=\"$name ()\"/>"; | |
| $tags .= "<tag k=\"name:en\" v=\"\"/>"; | |
| $tags .= "<tag k=\"name:ja\" v=\"$name\"/>"; | |
| $tags .= "<tag k=\"name:ja_rm\" v=\"\"/>"; | |
| my $node | |
| = sprintf("<node id=\"%d\" visible=\"true\" lat=\"%s\" lon=\"%s\">$tags</node>" | |
| , $negative_id, $centerHash{$string}{"lat"}, $centerHash{$string}{"long"}); | |
| print OSM "$node\n"; | |
| $num_places++; | |
| printf LOG "Node %d: %s, %s, %s, %s\n" , $negative_id, $place_tag | |
| , $string , $centerHash{$string}{"lat"}, $centerHash{$string}{"long"}; | |
| } | |
| sub write_node() { | |
| my ($i, $item, $coordinate) = @_; | |
| my $node_id = 0; | |
| my ($lat, $long) = split(/\s/, $coordinate); | |
| unless (defined($lat) && defined($long)) { | |
| print LOG "Skip error data : id = $item, coordinate = $coordinate\n"; | |
| print "Skip error data : id = $item, coordinate = $coordinate\n"; | |
| $num_error++; | |
| return $i; | |
| } | |
| if (($lat eq "") || ($long eq "")) { | |
| print LOG "Skip error data : id = $item, coordinate = $coordinate, lat = $lat, long = $long\n"; | |
| print "Skip error data : id = $item, coordinate = $coordinate, lat = $lat, long = $long\n"; | |
| $num_error++; | |
| return $i; | |
| } | |
| if (($lat > $north) || ($lat < $south) | |
| || ($long > $east) || ($long < $west)) { | |
| print LOG "Skip error data : id = $item, coordinate = $coordinate, lat = $lat, long = $long\n"; | |
| print "Skip error data : id = $item, coordinate = $coordinate, lat = $lat, long = $long\n"; | |
| $num_error++; | |
| return $i; | |
| } | |
| my $tags = &write_osm_constant_tag(); | |
| $tags .= "<tag k=\"KSJ2:curve_id\" v=\"$item\"/>"; | |
| $tags .= "<tag k=\"KSJ2:coordinate\" v=\"$coordinate\"/>"; | |
| $tags .= "<tag k=\"KSJ2:lat\" v=\"$lat\"/>"; | |
| $tags .= "<tag k=\"KSJ2:long\" v=\"$long\"/>"; | |
| if (exists($nodes{"$lat $long"})) { | |
| $node_id = $nodes{"$lat $long"}; | |
| } | |
| else { | |
| $negative_id--; | |
| $node_id = $negative_id; | |
| $nodes{"$lat $long"} = $node_id; | |
| my $node | |
| = sprintf("<node id=\"%d\" visible=\"true\" lat=\"%s\" lon=\"%s\">$tags</node>" | |
| , $node_id, $lat, $long); | |
| print OSM "$node\n"; | |
| $num_nodes++; | |
| } | |
| $node_ref .= sprintf("<nd ref=\"%d\" />", $node_id); | |
| printf LOG "Node %d: %s, %s\n", $node_id, $lat, $long; | |
| $i++; | |
| return $i; | |
| } | |
| sub write_way() { | |
| my ($flag, $i, $item) = @_; | |
| $negative_id--; | |
| my $tags = &write_osm_constant_tag(); | |
| $tags .= "<tag k=\"boundary\" v=\"administrative\"/>"; | |
| if ($flag eq "inner") {$tags .= "<tag k=\"admin_level\" v=\"8\"/>";} | |
| else {$tags .= "<tag k=\"admin_level\" v=\"6\"/>";} | |
| $tags .= "<tag k=\"KSJ2:curve_id\" v=\"$item\"/>"; | |
| # $tags .= "<tag k=\"KSJ2:points\" v=\"$curveHash{$item}{'points'}\"/>"; # comment out to reduce data size | |
| my $way | |
| = sprintf("<way id=\"%d\" action=\"modify\" visible=\"true\">$node_ref$tags</way>" | |
| , $negative_id); | |
| print OSM "$way\n"; | |
| $num_ways++; | |
| printf LOG "Way %d: %d nodes, curve id=%s\n" | |
| , $negative_id, $i, $item; | |
| $node_ref = ""; | |
| } | |
| # run this script. | |
| main(); | |
| # end of script | |
| __END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment