Skip to content

Commit

Permalink
Add a script to populate the tracker database with an ordered list of…
Browse files Browse the repository at this point in the history
… regions.
  • Loading branch information
gravitystorm committed Jun 25, 2012
1 parent f33da4d commit 89cda57
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bounds.xml
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- add as many bounds as you see fit -->
<!-- bounding boxes will be expanded into 1 degree squares -->
<!-- descriptions are ignored, but are here just to help coordination -->
<list>
<bounds minlat="51.2" minlon="-12.2524800" maxlat="55.5" maxlon="-5.29" description="Ireland" />
<bounds minlat="43.71" minlon="7.40" maxlat="43.76" maxlon="7.45" description="Monaco" />
</list>
5 changes: 5 additions & 0 deletions example.auth.yaml
Expand Up @@ -10,4 +10,9 @@ oauth:
token:
token_secret:
site: http://localhost:3000
tracker:
host: localhost
dbname: tracker
user: openstreetmap
password: osm
...
52 changes: 52 additions & 0 deletions run_regions.rb
@@ -0,0 +1,52 @@
#!/usr/bin/env ruby
# Generates the ordered region list in the tracker database

require 'pg'
require 'yaml'
require 'xml/libxml'

# Lat+lon are for the Bottom Left of the cell

@regions = {}

parser = XML::Reader.io(File.open('bounds.xml', "r"))

dbauth = YAML.load(File.open('auth.yaml'))['tracker']

@conn = PGconn.open( :dbname => dbauth['dbname'] )

@conn.exec("create table if not exists regions (id serial, lat integer, lon integer, status region_status, bot_id integer)")
@conn.exec("truncate table regions")

def add_region(lat, lon)
@conn.exec("insert into regions (lat, lon) values ($1, $2)", [lat, lon])
@regions[[lat, lon]] = true
end

@conn.transaction do
while parser.read do
next unless ["bounds"].include? parser.name

# you want to floor the maxes too, or do (ceil - 1)
min_lat = parser["minlat"].to_f.floor
max_lat = parser["maxlat"].to_f.floor
min_lon = parser["minlon"].to_f.floor
max_lon = parser["maxlon"].to_f.floor

(min_lat..max_lat).each do |lat|
(min_lon..max_lon).each do |lon|
add_region(lat, lon)
end
end
end

# Add remaining regions that weren't covered by the bounds
(-180..179).each do |lon|
(-90..89).each do |lat|
unless @regions.key?([lat, lon])
add_region(lat, lon)
end
end
end
end

0 comments on commit 89cda57

Please sign in to comment.