source: subversion/applications/utils/changeset2shp/changeset2shp.pl @ 34943

Last change on this file since 34943 was 27914, checked in by frederik, 8 years ago

new utility to make a shape file from something like changesets-latest.osm so you can quickly view areas of activity in e.g. qgis

File size: 1.5 KB
Line 
1#!/usr/bin/perl
2
3# reads a changeset XML on stdin (e.g. changeset-latest.osm)
4# and writes a shape file that contains the rectangle for each,
5# together with some attributes.
6
7# todo: add code that can also convert <tag> data to shape columns.
8# add code to filter only certain changesets.
9
10# written by Frederik Ramm <frederik@remote.org>, public domain.
11
12use Geo::Shapelib qw/:all/;
13use strict;
14
15my $shapefile = new Geo::Shapelib { 
16    Name => 'changesets',
17    Shapetype => POLYGON,
18    FieldNames => ['id','who','opened','closed','num_edits'],
19    FieldTypes => ['Integer:8','String:50','String:20','String:20','Integer:5']
20};
21
22while (<>) {
23    chomp;
24    next unless (/<changeset.*\sid="(\d+)"/);
25    my $id = $1;
26    next unless (/\suser="([^"]+)"/);
27    my $user = $1;
28    next unless (/\screated_at="([^"]+)"/);
29    my $cra = $1;
30    next unless (/\sclosed_at="([^"]+)"/);
31    my $cla = $1;
32    next unless (/\snum_changes="(\d+)"/);
33    my $nc = $1;
34    my %bb;
35    foreach my $attr(qw/min_lat min_lon max_lon max_lat/)
36    {
37        last unless (/\s$attr="([0-9.-]+)"/);
38        $bb{$attr} = $1;
39    }
40    next unless defined($bb{'max_lat'});
41
42    my $nc = $1;
43
44    push @{$shapefile->{Shapes}},{ Vertices => [
45        [$bb{min_lon}, $bb{min_lat}, 0, 0],
46        [$bb{max_lon}, $bb{min_lat}, 0, 0],
47        [$bb{max_lon}, $bb{max_lat}, 0, 0],
48        [$bb{min_lon}, $bb{max_lat}, 0, 0],
49        [$bb{min_lon}, $bb{min_lat}, 0, 0]
50     ] };
51    push @{$shapefile->{ShapeRecords}}, [$id, $user, $cra, $cla, $nc];
52}
53
54$shapefile->save();
Note: See TracBrowser for help on using the repository browser.