-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfs-major-incident-georss-to-geojson.pl
executable file
·281 lines (230 loc) · 8.49 KB
/
rfs-major-incident-georss-to-geojson.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/perl -w
# This is a custom GeoRSS to GeoJSON converter specifically written for the NSW
# RFS Major Incident Feed at http://www.rfs.nsw.gov.au/feeds/majorIncidents.xml
# Originally I used ogr2ogr to convert the GeoRSS to GeoJSON however because the
# RFS feed at worst case doesn't adhere to the GeoRSS specification and at best
# case makes an interesting use of the specification, the OGR driver simply
# doesn't handle the RFS GeoRSS file as I need it too.
#
# Specifically the RFS Major Incidents GeoRSS file contains several
# <georss:point> or <georss:polygon> elements per RSS item. We want these
# represented in the GeoJSON file as GeometryCollections retaining all the
# <georss:point> and <georss:polygon> elements.
#
# Since there is no way to get in touch with the right people at RFS to discuss
# this issue, and even if I did get in touch I doubt they would just roll out a
# better solution quickly I have hacked together this conversion script.
#
# The good thing is that with my testing so far it seems to work very well as
# required. The bad thing is I haven't made it as robust and error proof as
# possible.
#
# I did try at least 3 RSS reader modules in Perl, but two crashed when trying
# to read the GeoRSS and one had a problem with reading multiple
# <georss:polygon> elements (it actually concatenated all the values, so
# coordinate pair lists together which is really wrong), but a plain old
# XML::Simple parser works great, especially since it is hard coded for the RFS
# GeoRSS feed.
# This script is released under the Creative Commons Zero CC0 license
#
# To the extent possible under law, the person who associated CC0
# with this work has waived all copyright and related or neighboring
# rights to this work.
# http://creativecommons.org/publicdomain/zero/1.0/
use warnings;
use strict;
# library to parse source RSS file
use XML::Simple;
# library to generate destination JSON file
use JSON;
# check program usage
if (@ARGV < 2) {
print STDERR "Usage: $0 <majorIncidents.xml> <majorIncidents.geojson>\n";
exit 1;
}
# take the first argument as the source georss file
my $georss_file = $ARGV[0];
#
# take the second argument as the destination geojson file
my $geojson_file = $ARGV[1];
# ensure we can read the source georss file
if ( ! -r $georss_file ) {
print STDERR "'$georss_file' can't be read.\n";
exit 1;
}
# XML::Parser doesn't support UTF-16
# See http://stackoverflow.com/questions/28100716/is-there-a-way-to-get-xmltwig-to-understand-a-utf-16-encoded-xml-file
use XML::LibXML qw( );
my $xmlstring;
{
open(my $fh, '<:raw', $ARGV[0])
or die $!;
local $/;
$xmlstring = <$fh>;
}
{
#my $doc = XML::LibXML->new()->parse_string($xmlstring);
#$doc->setEncoding('UTF-8');
#$xmlstring = $doc->toString();
$xmlstring =~ s/encoding="utf-16"/encoding="utf-8"/;
}
print STDERR "Converted to UTF-8\n";
# parse the RSS file using an XML parser
my $xml = XML::Simple->new();
my $doc = $xml->XMLin($xmlstring);
# during development it was handy to Dump the whole $doc structure
#use Data::Dumper;
#print Dumper ($doc);
#exit;
# helper function to take a GeoRSS Point and Polygon coordinate pair string list
# and generate GeoJSON geometries
sub generateGeometries($$) {
my ($point, $polygon) = @_;
# GeoJSON geometry object
my @geometries = ();
my @points;
if (ref($point) eq "ARRAY") { # many points
push @points, @{$point};
} else {
push @points, $point;
}
# for each point geometry
foreach my $p (@points) {
my $g = georss_to_geojson_point($p);
if (defined $g) {
push @geometries, $g;
}
}
my @polygons;
if (ref($polygon) eq "ARRAY") { # many polygons
push @polygons, @{$polygon};
} else {
push @polygons, $polygon;
}
foreach my $p (@polygons) {
my $g = georss_to_geojson_polygon($p);
if (defined $g) {
push @geometries, $g;
}
}
return \@geometries;
}
# helper function to take a GeoRSS Polygon <georss:polygon> string value and
# create a GeoJSON geometry object
sub georss_to_geojson_polygon($) {
my ($s) = @_;
my @georss_coords = split(/ /, $s);
# check coords list is even otherwise we don't have a complete list of
# coordinate pairs
if ((scalar @georss_coords % 2) != 0) {
print STDERR "WARNING: Coordinate pair list is odd, so we don't have a complete paired list.\n";
return undef;
}
my @coords;
for (my $i = 0; $i < scalar @georss_coords; $i += 2) {
my ($lat, $lon) = ($georss_coords[$i], $georss_coords[$i+1]);
$lat = $lat + 0; # force as number for JSON writer
$lon = $lon + 0;
push @coords, [ $lon, $lat ]
}
if (scalar @coords < 4) {
print STDERR "WARNING: Expecting to produce a LinearRing but only " . scalar @coords . " coordinate pairs found.\n";
return undef;
}
# FIXME add check for 1st point == last point
my $g = {
'type' => 'Polygon',
'coordinates' => [ \@coords ] # just paste in coordinates as exterior rings each time
};
return $g;
}
# helper function to take a GeoRSS Point <georss:point> string value and
# create a GeoJSON geometry object
sub georss_to_geojson_point($) {
my ($s) = @_;
my ($lat, $lon) = split(/ /, $s);
if (!defined $lat || !defined $lon) {
print STDERR "WARNING: Point element but less than 2 coordinates in the list.\n";
return undef;
}
$lat = $lat + 0; # force as number for JSON writer
$lon = $lon + 0;
my $g = {
'type' => 'Point',
'coordinates' => [ $lon, $lat ]
};
return $g;
}
# store for target json features (from rss items)
my @features = ();
# for each RSS item (i.e. each major incident)...
foreach my $item (@{$doc->{'channel'}->{'item'}}) {
# each geojson feature shall have properties and an optional geometry
# here we pull the properties from the rss item elements
my %properties = ();
foreach my $element (qw/category description title link pubDate/) {
if (defined $item->{$element}) {
$properties{$element} = $item->{$element};
}
}
if (defined $item->{'guid'} && defined $item->{'guid'}->{'isPermaLink'}) {
$properties{'guid_isPermaLink'} = $item->{'guid'}->{'isPermaLink'};
}
if (defined $item->{'guid'} && defined $item->{'guid'}->{'content'}) {
$properties{'guid'} = $item->{'guid'}->{'content'};
}
# here we pull and construct the geometry
my $geometry = {};
# if GeoRSS elements are present for this feature
if (defined $item->{'georss:point'} || defined $item->{'georss:polygon'}) {
# FIXME could simplify this...
my $number_of_points = 0;
my $number_of_polygons = 0;
if (defined $item->{'georss:point'}) {
if (ref($item->{'georss:point'}) eq "ARRAY") {
$number_of_points += scalar @{$item->{'georss:point'}};
} else {
$number_of_points += 1;
}
}
if (defined $item->{'georss:polygon'}) {
if (ref($item->{'georss:polygon'}) eq "ARRAY") {
$number_of_polygons += scalar @{$item->{'georss:polygon'}};
} else {
$number_of_polygons += 1;
}
}
my $number_of_geometries = $number_of_points + $number_of_polygons;
if ($number_of_geometries == 1) {
if ($number_of_points == 1) {
$geometry = georss_to_geojson_point($item->{'georss:point'});
} elsif ($number_of_polygons == 1) {
$geometry = georss_to_geojson_polygon($item->{'georss:polygon'});
} else {
# programming error
$geometry = undef;
}
} else {
$geometry = {
'type' => 'GeometryCollection',
'geometries' => generateGeometries($item->{'georss:point'}, $item->{'georss:polygon'})
};
}
} else {
$geometry = undef;
}
my %feature = (
'type' => 'Feature',
'properties' => \%properties
);
# if the feature had georss geometries then also add a geometry to the
# geojson
if (defined $geometry) {
$feature{'geometry'} = \%{$geometry};
}
push @features, \%feature;
}
my $geojson = { 'type' => "FeatureCollection" };
$geojson->{'features'} = \@features;
open (my $geojson_fh, ">", $geojson_file) or die "Can't open '$geojson_file' for writting.\n";
print $geojson_fh encode_json($geojson) . "\n";