Permalink
Browse files

Added SwissMatch::Districts and SwissMatch::District, along with info…

…rmation, datafiles and loading mechanism.
  • Loading branch information...
1 parent e341cf8 commit 407016705a373b8860acdc7dae60adce8141241f @apeiros committed Sep 17, 2012
@@ -0,0 +1,148 @@
+GDEKT,GDEBZNR,GDEBZNA
+ZH,101,Bezirk Affoltern
+ZH,102,Bezirk Andelfingen
+ZH,103,Bezirk Bülach
+ZH,104,Bezirk Dielsdorf
+ZH,105,Bezirk Hinwil
+ZH,106,Bezirk Horgen
+ZH,107,Bezirk Meilen
+ZH,108,Bezirk Pfäffikon
+ZH,109,Bezirk Uster
+ZH,110,Bezirk Winterthur
+ZH,111,Bezirk Dietikon
+ZH,112,Bezirk Zürich
+BE,241,Arrondissement administratif Jura bernois
+BE,242,Verwaltungskreis Biel/Bienne
+BE,243,Verwaltungskreis Seeland
+BE,244,Verwaltungskreis Oberaargau
+BE,245,Verwaltungskreis Emmental
+BE,246,Verwaltungskreis Bern-Mittelland
+BE,247,Verwaltungskreis Thun
+BE,248,Verwaltungskreis Obersimmental-Saanen
+BE,249,Verwaltungskreis Frutigen-Niedersimmental
+BE,250,Verwaltungskreis Interlaken-Oberhasli
+LU,301,Amt Entlebuch
+LU,302,Amt Hochdorf
+LU,303,Amt Luzern
+LU,304,Amt Sursee
+LU,305,Amt Willisau
+UR,400,Kanton Uri
+SZ,501,Bezirk Einsiedeln
+SZ,502,Bezirk Gersau
+SZ,503,Bezirk Höfe
+SZ,504,Bezirk Küssnacht (SZ)
+SZ,505,Bezirk March
+SZ,506,Bezirk Schwyz
+OW,600,Kanton Obwalden
+NW,700,Kanton Nidwalden
+GL,800,Kanton Glarus
+ZG,900,Kanton Zug
+FR,1001,District de la Broye
+FR,1002,District de la Glâne
+FR,1003,District de la Gruyère
+FR,1004,District de la Sarine
+FR,1005,Bezirk See / District du Lac
+FR,1006,Bezirk Sense
+FR,1007,District de la Veveyse
+SO,1101,Bezirk Gäu
+SO,1102,Bezirk Thal
+SO,1103,Bezirk Bucheggberg
+SO,1104,Bezirk Dorneck
+SO,1105,Bezirk Gösgen
+SO,1106,Bezirk Wasseramt
+SO,1107,Bezirk Lebern
+SO,1108,Bezirk Olten
+SO,1109,Bezirk Solothurn
+SO,1110,Bezirk Thierstein
+BS,1200,Kanton Basel-Stadt
+BL,1301,Bezirk Arlesheim
+BL,1302,Bezirk Laufen
+BL,1303,Bezirk Liestal
+BL,1304,Bezirk Sissach
+BL,1305,Bezirk Waldenburg
+SH,1401,Bezirk Oberklettgau
+SH,1402,Bezirk Reiat
+SH,1403,Bezirk Schaffhausen
+SH,1404,Bezirk Schleitheim
+SH,1405,Bezirk Stein
+SH,1406,Bezirk Unterklettgau
+AR,1501,Bezirk Hinterland
+AR,1502,Bezirk Mittelland
+AR,1503,Bezirk Vorderland
+AI,1600,Kanton Appenzell Innerrhoden
+SG,1721,Wahlkreis St. Gallen
+SG,1722,Wahlkreis Rorschach
+SG,1723,Wahlkreis Rheintal
+SG,1724,Wahlkreis Werdenberg
+SG,1725,Wahlkreis Sarganserland
+SG,1726,Wahlkreis See-Gaster
+SG,1727,Wahlkreis Toggenburg
+SG,1728,Wahlkreis Wil
+GR,1821,Bezirk Albula
+GR,1822,Distretto di Bernina
+GR,1823,Bezirk Hinterrhein
+GR,1824,Bezirk Imboden
+GR,1825,Bezirk Inn
+GR,1826,Bezirk Landquart
+GR,1827,Bezirk Maloja / Distretto di Maloggia
+GR,1828,Distretto di Moesa
+GR,1829,Bezirk Plessur
+GR,1830,Bezirk Prättigau-Davos
+GR,1831,Bezirk Surselva
+AG,1901,Bezirk Aarau
+AG,1902,Bezirk Baden
+AG,1903,Bezirk Bremgarten
+AG,1904,Bezirk Brugg
+AG,1905,Bezirk Kulm
+AG,1906,Bezirk Laufenburg
+AG,1907,Bezirk Lenzburg
+AG,1908,Bezirk Muri
+AG,1909,Bezirk Rheinfelden
+AG,1910,Bezirk Zofingen
+AG,1911,Bezirk Zurzach
+TG,2011,Bezirk Arbon
+TG,2012,Bezirk Frauenfeld
+TG,2013,Bezirk Kreuzlingen
+TG,2014,Bezirk Münchwilen
+TG,2015,Bezirk Weinfelden
+TI,2101,Distretto di Bellinzona
+TI,2102,Distretto di Blenio
+TI,2103,Distretto di Leventina
+TI,2104,Distretto di Locarno
+TI,2105,Distretto di Lugano
+TI,2106,Distretto di Mendrisio
+TI,2107,Distretto di Riviera
+TI,2108,Distretto di Vallemaggia
+VD,2221,District d'Aigle
+VD,2222,District de la Broye-Vully
+VD,2223,District du Gros-de-Vaud
+VD,2224,District du Jura-Nord vaudois
+VD,2225,District de Lausanne
+VD,2226,District de Lavaux-Oron
+VD,2227,District de Morges
+VD,2228,District de Nyon
+VD,2229,District de l'Ouest lausannois
+VD,2230,District de la Riviera-Pays-d'Enhaut
+VS,2301,Bezirk Brig
+VS,2302,District de Conthey
+VS,2303,District d'Entremont
+VS,2304,Bezirk Goms
+VS,2305,District d'Hérens
+VS,2306,Bezirk Leuk
+VS,2307,District de Martigny
+VS,2308,District de Monthey
+VS,2309,Bezirk Raron
+VS,2310,District de Saint-Maurice
+VS,2311,District de Sierre
+VS,2312,District de Sion
+VS,2313,Bezirk Visp
+NE,2401,District de Boudry
+NE,2402,District de la Chaux-de-Fonds
+NE,2403,District du Locle
+NE,2404,District de Neuchâtel
+NE,2405,District du Val-de-Ruz
+NE,2406,District du Val-de-Travers
+GE,2500,Canton de Genève
+JU,2601,District de Delémont
+JU,2602,District des Franches-Montagnes
+JU,2603,District de Porrentruy
View
@@ -0,0 +1,4 @@
+Location of zip code files:
+ http://match.post.ch
+Location of community <-> zip code mapping file:
+ http://www.bfs.admin.ch/bfs/portal/de/index/infothek/nomenklaturen/blank/blank/gem_liste/04.Document.82420.xls
View
@@ -0,0 +1,82 @@
+# encoding: utf-8
+
+
+
+module SwissMatch
+
+ # Represents a swiss district.
+ class District
+
+ # @return [String]
+ # The district number.
+ attr_reader :district_number
+
+ # @return [String]
+ # The name of the district.
+ attr_reader :name
+
+ # @return [SwissMatch::Communities]
+ # The political communities belonging to this district
+ attr_reader :communities
+
+ attr_reader :canton
+
+ # @param [String] district_number
+ # The two letter abbreviation of the districts name as used on license plates.
+ # @param [String] name
+ # The official name of the district.
+ # @param [SwissMatch::Canton] canton
+ # The canton this district belongs to
+ # @param [SwissMatch::Communities] communities
+ # The communities belonging to this district
+ def initialize(district_number, name, canton, communities)
+ @district_number = district_number
+ @name = name
+ @canton = canton
+ @communities = communities
+ end
+
+ # @param [Boolean] retain_references
+ # If set to false, :agglomeration will be set to the community_number and
+ # :canton to the canton's license_tag.
+ #
+ # @return [Hash]
+ # All properties of the district as a hash.
+ def to_hash(retain_references=false)
+ if retain_references
+ canton = @canton
+ communities = @communities
+ else
+ canton = @canton && @canton.license_tag
+ communities = @communities.map(&:community_number)
+ end
+
+ {
+ :name => @name,
+ :district_number => @district_number,
+ :canton => canton,
+ :communities => communities,
+ }
+ end
+
+ alias to_s name
+
+ # @private
+ # @see Object#hash
+ def hash
+ [self.class, @number].hash
+ end
+
+ # @private
+ # @see Object#eql?
+ def eql?(other)
+ self.class.eql?(other.class) && @number.eql?(other.number)
+ end
+
+ # @return [String]
+ # @see Object#inspect
+ def inspect
+ sprintf "\#<%s:%014x %d %p>", self.class, object_id, @district_number, to_s
+ end
+ end
+end
View
@@ -0,0 +1,119 @@
+# encoding: utf-8
+
+
+
+require 'swissmatch/district'
+
+
+
+module SwissMatch
+
+ # Represents a collection of swiss districts and provides a query interface.
+ class Districts
+ include Enumerable
+
+ # @param [Array<SwissMatch::District>] districts
+ # The SwissMatch::District objects this SwissMatch::Districts should contain
+ def initialize(districts)
+ @districts = districts
+ @by_district_number = {}
+ @by_name = {}
+
+ districts.each do |district|
+ @by_district_number[district.district_number] = district
+ @by_name[district.name] = district
+ end
+ end
+
+ # Calls the block once for every SwissMatch::District in this SwissMatch::Districts
+ # instance, passing that district as a parameter.
+ # The order is the same as the instance was constructed.
+ #
+ # @yield [district]
+ # @yieldparam [SwissMatch::District] district
+ #
+ # @return [self] Returns self
+ def each(&block)
+ @districts.each(&block)
+ self
+ end
+
+ # Calls the block once for every SwissMatch::District in this SwissMatch::Districts
+ # instance, passing that district as a parameter.
+ # The order is the reverse of what the instance was constructed.
+ #
+ # @yield [district]
+ # @yieldparam [SwissMatch::District] district
+ #
+ # @return [self] Returns self
+ def reverse_each(&block)
+ @districts.reverse_each(&block)
+ self
+ end
+
+ # @return [SwissMatch::Districts]
+ # A SwissMatch::Districts collection with all SwissMatch::District objects for which the block
+ # returned true (or a trueish value)
+ def select(*args, &block)
+ Districts.new(@districts.select(*args, &block))
+ end
+
+ # @return [SwissMatch::Districts]
+ # A SwissMatch::Districts collection with all SwissMatch::District objects for which the block
+ # returned false (or a falseish value)
+ def reject(*args, &block)
+ Districts.new(@districts.reject(*args, &block))
+ end
+
+ # @see Enumerable#sort
+ #
+ # @return [SwissMatch::Districts]
+ # A SwissMatch::Districts collection sorted by the block
+ def sort(*args, &block)
+ Districts.new(@districts.sort(*args, &block))
+ end
+
+ # @see Enumerable#sort_by
+ #
+ # @return [SwissMatch::Districts]
+ # A SwissMatch::Districts collection sorted by the block
+ def sort_by(*args, &block)
+ Districts.new(@districts.sort_by(*args, &block))
+ end
+
+ # @return [SwissMatch::District]
+ # The district with the given district_number or name
+ def [](district_number_or_name)
+ @by_district_number[district_number_or_name] || @by_name[district_number_or_name]
+ end
+
+ # @return [SwissMatch::District]
+ # The district with the given license tag.
+ def by_district_number(tag)
+ @by_district_number[tag]
+ end
+
+ # @return [SwissMatch::District]
+ # The district with the given name (any language)
+ def by_name(name)
+ @by_name[name]
+ end
+
+ # @return [Integer] The number of SwissMatch::District objects in this collection.
+ def size
+ @districts.size
+ end
+
+ # @return [Array<SwissMatch::District>]
+ # An Array with all SwissMatch::District objects in this SwissMatch::Districts.
+ def to_a
+ @districts.dup
+ end
+
+ # @private
+ # @see Object#inspect
+ def inspect
+ sprintf "\#<%s:%x size: %d>", self.class, object_id>>1, size
+ end
+ end
+end
View
@@ -57,6 +57,21 @@ def self.cantons
@data.cantons
end
+ # @param [String] district_number_or_name
+ # The district_number or name of the district
+ #
+ # @return [SwissMatch::District]
+ # The district with the given district_number or name
+ def self.district(district_number_or_name)
+ @data.districts[district_number_or_name]
+ end
+
+ # @return [SwissMatch::Districts]
+ # All known districts
+ def self.districts
+ @data.districts
+ end
+
# @param [Integer] key
# The community number of the community
#
@@ -204,6 +219,16 @@ def self.cantons(*args, &block)
SwissMatch::Location.cantons(*args, &block)
end
+ # @see SwissMatch::Location::district
+ def self.district(*args, &block)
+ SwissMatch::Location.district(*args, &block)
+ end
+
+ # @see SwissMatch::Location::districts
+ def self.districts(*args, &block)
+ SwissMatch::Location.districts(*args, &block)
+ end
+
# @see SwissMatch::Location::community
def self.community(*args, &block)
SwissMatch::Location.community(*args, &block)
Oops, something went wrong.

0 comments on commit 4070167

Please sign in to comment.