User:A1kmm/NZPlaces

The following small Python script will generate an HTML page which can initialise New Zealand suburbs to a basic stub. It uses the geographic place names database from LINZ, and the census data from Statistics New Zealand to generate a reasonable attempt at a stub.

It has some hard-coded references to Auckland, so make sure you change these before you use the script for another region!

Paste into a .py file and run the script to use. You need landdist.txt from linz.govt.nz, and you need to make a Population.csv file. To do this, get the census population data for the cities you need(they are in XLS format). Copy the name, population 2001 to columns 1 and 2, and make a column 3 with the city name. Save this as CSV to Populations.csv. Then run python wikigen.py >suburbs.html. Next visit suburbs.html in your browser, and click the button for the suburb you want to initialise. You will see a pre-filled-in preview, which you can then save. Go Back and repeat until all suburbs are done.

If anyone has already created the suburb, you will get an error on save saying that there is an edit conflict. If the page is already high quality, just leave it, otherwise you can manually merge the pages to make a better page.

your_area_north=6481227.20 your_area_east=2669329.90
 * 1) Set this to the area you know lots about, and it will pull out all the
 * 2) suburbs close to it.

your_comfortable_radius=20000 your_safe_radius=your_comfortable_radius+4000
 * 1) How close to your area do you know enough about?
 * 2) (in m)
 * 1) How much further should we go to look for close matches(for example, for the
 * 2) north, east, south, west of other suburbs. These suburbs only show up as
 * 3) links, they don't get their own entry.

import math import string import re

class CompassPoint: def __init__(self, distance, direction, record): self.distance = distance self.direction = direction self.record = record

class GeoRecord: def __init__(self, name, north, east, hide): self.name = string.capwords(name) self.north = north self.east = east self.hide = hide self.population = 0 self.council = None

def DistanceTo(self, gr): # Standard Euclidean distance... return (pow(pow(self.north - gr.north, 2) + pow(self.east - gr.east, 2), 0.5))

def AngleTo(self, gr): n = gr.north - self.north e = gr.east - self.east a = math.atan2(e, n) * 180.0 / math.pi       if (a < 0): a = 360.0 + a       return (a)

def DirectionTo(self, gr): n = gr.north - self.north e = gr.east - self.east # Don't let tiny distances affect the direction. if (n n): n = 0 if (e e): e = 0 ang = math.atan2(e, n) * 180.0 / math.pi       if (ang < 0): ang = 360.0 + ang ang = self.AngleTo(gr) if ang > 337.5 or ang < 22.5: return "north" if ang < 67.5: return "north-east" if ang < 112.5: return "east" if ang < 157.5: return "south-east" if ang < 202.5: return "south" if ang < 247.5: return "south-west" if ang < 292.5: return "west" return "north-west"

def GetCompass(self, reclist): compass = {}; for rec in reclist: dist = self.DistanceTo(rec) dirto = self.DirectionTo(rec) if (dist > 4000): continue if (rec.name == self.name): continue if compass.has_key(dirto) and compass[dirto].distance < dist: continue compass[dirto] = CompassPoint(dist, dirto, rec) return (compass)

def MakeLink(self): return "" + self.name + ""

def GetCity(self): if self.council: return "" + self.council + "" return 'Please help Wikipedia by filling this in'

reclist = [] byname = {} f = file("namedata.txt", "r") f.readline while True: l=f.readline if l == "": break l = l.rstrip("\r\n") ar = l.split('`') fname = ar[1] ftype = ar[4] fwhere = ar[5] if ftype != 'SBRB' and ftype != 'USAT' and ftype != 'METR': continue feast = float(ar[2]) fnorth = float(ar[3]) rya = pow(pow((fnorth - your_area_north),2) + pow((feast - your_area_east),2), 0.5) if (rya > your_safe_radius): continue hideflag = (rya > your_comfortable_radius)
 * 1) Read in the file...
 * 1) Skip header...

r = GeoRecord(fname, fnorth, feast, hideflag) reclist.append(r) byname[r.name] = r   # print ("Name: " + fname + " type " + ftype +\    #       " where " + fwhere + " north %f" +\    #      " east %f " + " hideflag = %d") % (fnorth, feast, hideflag) f.close f = file("Populations.csv", "r") f.readline while True: l=f.readline if l == "": break l = l.rstrip("\r\n") ar = l.split(',') fname = ar[0].lstrip('"').rstrip('"') fpopulation = ar[1] fcouncil = ar[2].lstrip('"').rstrip('"') # The name needs some cleaning up... m = re.match(r"[0-9]+ ([^\n]+) (North|South|East|West)", fname) doneflag = False if (m != None): cfname = m.group(1) if byname.has_key(cfname): r = byname[cfname] r.population = r.population + int(fpopulation) r.council = fcouncil doneflag = True
 * 1) Read in the population database...
 * 1) Skip header...

# Now try without stripping North, South, East, West if not doneflag: m = re.match(r"[0-9]+ ([^\n]+)", fname) fname = m.group(1) if byname.has_key(fname): r = byname[fname] r.population = r.population + int(fpopulation) r.council = fcouncil

f.close

def PrettyDistance(d): if d < 1500: return "%d m" % d   return "%0.1f km" % round(d/1000,1)

def MakeInfoBox(suburb): compass = suburb.GetCompass(reclist) infobox = ''' '''   return infobox

import cgi;

print " " for suburb in reclist: if suburb.hide: continue page = MakeInfoBox(suburb) page = page + "" + suburb.name + " is an " +\ "Auckland suburb.\n"
 * 1) Now, go through each suburb, and write an entry...

if (suburb.council != None): page = page + "\n" + suburb.name +\ " is under the local governance of the " + suburb.council +\              " City Council.\n" +\ "\nAccording to the 2001 census, " + suburb.name +\ " has a population of %d" % suburb.population + ".\n"

page = page + "\n" +\ "\n" print "" print "" print "" print "" print "" print "" print "" print " " print " "