-
Notifications
You must be signed in to change notification settings - Fork 6
/
kml2geojson_dist.pl
86 lines (71 loc) · 2.12 KB
/
kml2geojson_dist.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/perl
use XML::Hash::LX;
#use JSON;
# KML file from http://daticos-geotec.opendata.arcgis.com/datasets/741bdd9fa2ca4d8fbf1c7fe945f8c916_0
open(IN, '<kml/distritos.kml');
while ( $l = <IN> )
{
$xml .= $l;
}
my $hash = xml2hash $xml;
$hash = $hash->{kml}->{Document}->{Folder};
@pms = @{ $hash->{Placemark} };
foreach $pm ( @pms )
{
#use Data::Dumper;
@data = @{ $pm->{ExtendedData}->{SchemaData}->{SimpleData} };
foreach $data ( @data )
{
if ( $data->{'-name'} eq 'NOM_PROV')
{
$p = $data->{'#text'};
}
elsif ( $data->{'-name'} eq 'NOM_CANT')
{
$c = $data->{'#text'};
}
elsif ( $data->{'-name'} eq 'NOM_DIST')
{
$d = $data->{'#text'};
}
elsif ( $data->{'-name'} eq 'CODIGO')
{
$code = $data->{'#text'};
if ( $code =~ /^(\d\d\d)(\d)$/ )
{
$code = $1.'0'.$2;
}
}
}
print "$p - $c - $d: $code\n";
$coords = $pm->{Polygon}->{outerBoundaryIs}->{LinearRing}->{coordinates};
@coords = split(/\s/, $coords);
my @latlon;
foreach $coord ( @coords )
{
($lon,$lat) = split(/,/, $coord);
$lat += 0;
$lon += 0;
#push @latlon, [$lat, $lon];
push @latlon, [$lon, $lat];
}
$out{$code}{p} = $p;
$out{$code}{c} = $c;
$out{$code}{d} = $d;
push @{ $out{$code}{polys} }, \@latlon;
#print join(' | ', map { "$_ ".ref($hash->{$_}) } sort keys $pm->{ExtendedData})."\n";
#print $pm->{ExtendedData};
}
use JSON;
$JSON = JSON->new();
foreach $code ( sort keys %out )
{
$json = {
'type' => 'Feature',
'properties' => { Provincia => $out{$code}{p}, Canton => $out{$code}{c}, Distrito => $out{$code}{d}, Codigo => $code },
'geometry' => { type => MultiPolygon, coordinates => [ $out{$code}{polys} ] }
#'geometry' => { type => Polygon, coordinates => [ $out{$code}{polys}->[0] ] }
};
open(OUT, ">geojson/$code.geojson");
print OUT $JSON->pretty->encode($json);
}