Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
language: python
python:
- "2.6"
- "2.7"
- "3.6"
# command to install dependencies
install:
- pip install pep8 pyflakes pylint --use-mirrors
- pip install coverage coveralls --use-mirrors
- pip install pep8 pyflakes pylint
- pip install coverage coveralls
script: make test coverage
after_success:
- coveralls
Expand Down
20,231 changes: 20,231 additions & 0 deletions data/AT.tab

Large diffs are not rendered by default.

3,843 changes: 3,843 additions & 0 deletions data/CH.tab

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions plz_draw
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Canvas:
ctx.set_antialias(cairo.ANTIALIAS_DEFAULT)
# Basic scaling so that the context is axexctly 1 unit in the smaller dimension
# and a little bit more (depending on the aspect ratio) in the other
#self.ctxscale = min([self.width, self.height])
# self.ctxscale = min([self.width, self.height])
# hack to better fit germany - remove the next line if you get stupid results
self.ctxscale = max([self.width, self.height])
ctx.scale(self.ctxscale, self.ctxscale)
Expand Down Expand Up @@ -109,7 +109,7 @@ def find_dimensions(geoitems, deutschgrenzen):
# find dimenions of data set
x = []
y = []
#for plz, (lon, lat, name) in geoitems:
# for plz, (lon, lat, name) in geoitems:
# x.append(lon)
# y.append(lat)
for track in deutschgrenzen:
Expand Down
21,618 changes: 12,906 additions & 8,712 deletions pygeodb/plzdata.py

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions pygeodb/pygeodb_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
distanz_liste = sort(locations, referenz)
"""

from __future__ import absolute_import
from __future__ import absolute_import, print_function, unicode_literals

import math

from pygeodb.plzdata import geodata


Expand All @@ -38,7 +39,8 @@ def __init__(self, plz, land='DE'):
"""The python object for a geolocation"""
self.plz = plz
self.land = land
(self.longitude, self.latitude, self.city) = geodata.get(land.upper(), {}).get(plz, (0, 0, None))
(self.longitude, self.latitude, self.city) = geodata.get(
land.upper(), {}).get(plz, (0, 0, None))
if not self.city:
raise ValueError("Unknown PLZ: %s-%s" % (land, plz))

Expand All @@ -47,13 +49,15 @@ def __sub__(self, other):
fLat, fLon = math.radians(self.latitude), math.radians(self.longitude)
tLat, tLon = math.radians(other.latitude), math.radians(other.longitude)
distance = math.acos(
math.sin(tLat) * math.sin(fLat)
+ math.cos(tLat) * math.cos(fLat) * math.cos(tLon - fLon)) * 6380000
round(math.sin(tLat) * math.sin(fLat) +
math.cos(tLat) * math.cos(fLat) * math.cos(tLon - fLon), 10)) * 6380000
return int(distance)


def _obj2plz(obj):
if hasattr(obj, 'plz'):
if isinstance(obj, PLZ):
return obj
elif hasattr(obj, 'plz'):
return PLZ(obj.plz)
elif hasattr(obj, 'get') and obj.get('plz', None):
return PLZ(obj['plz'])
Expand Down
44 changes: 25 additions & 19 deletions pygeodb/voronoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@
#
#############################################################################

from __future__ import print_function

def cmp(a, b):
"""
Just for backwards compatibility...
"""
return a.__cmp__(b)

def next_or_none(i):
try:
return next(i)
except StopIteration:
return None

def usage():
print("""
voronoi - compute Voronoi diagram or Delaunay triangulation
Expand Down Expand Up @@ -181,19 +195,19 @@ def outEdge(self, edge):
if self.plot:
self.clip_line(edge)
elif(self.doPrint):
print "e %d" % edge.edgenum,
print " %d " % sitenumL,
print "%d" % sitenumR
print("e %d" % edge.edgenum, end='')
print(" %d " % sitenumL, end='')
print("%d" % sitenumR)

#------------------------------------------------------------------
def voronoi(siteList, context):
edgeList = EdgeList(siteList.xmin, siteList.xmax, len(siteList))
priorityQ = PriorityQueue(siteList.ymin, siteList.ymax, len(siteList))
siteIter = siteList.iterator()

bottomsite = siteIter.next()
bottomsite = next_or_none(siteIter)
context.outSite(bottomsite)
newsite = siteIter.next()
newsite = next_or_none(siteIter)
minpt = Site(-BIG_FLOAT, -BIG_FLOAT)
while True:
if not priorityQ.isEmpty():
Expand Down Expand Up @@ -238,7 +252,7 @@ def voronoi(siteList, context):
# push the Halfedge into the ordered linked list of vertices
priorityQ.insert(bisector, p, newsite.distance(p))

newsite = siteIter.next()
newsite = next_or_none(siteIter)

elif not priorityQ.isEmpty():
# intersection is smallest - this is a vector (circle) event
Expand Down Expand Up @@ -341,6 +355,7 @@ def dump(self):
print("Site #%d (%g, %g)" % (self.sitenum, self.x, self.y))

def __cmp__(self, other):
# lambda s: (s.y, -s.x)
if self.y < other.y:
return -1
elif self.y > other.y:
Expand Down Expand Up @@ -437,7 +452,7 @@ def dump(self):
print("right: ", self.right)
print("edge: ", self.edge)
print("pm: ", self.pm)
print "vertex: ",
print("vertex: ", end='')
if self.vertex: self.vertex.dump()
else: print("None")
print("ystar: ", self.ystar)
Expand Down Expand Up @@ -707,26 +722,17 @@ def __init__(self, pointList):
if pt.y < self.__ymin: self.__ymin = pt.y
if pt.x > self.__xmax: self.__xmax = pt.x
if pt.y > self.__ymax: self.__ymax = pt.y
self.__sites.sort()
self.__sites.sort(key=lambda s: (s.y, -s.x))

def setSiteNumber(self, site):
site.sitenum = self.__sitenum
self.__sitenum += 1

class Iterator(object):
def __init__(this, lst): this.generator = (s for s in lst)
def __iter__(this): return this
def next(this):
try:
return this.generator.next()
except StopIteration:
return None

def iterator(self):
return SiteList.Iterator(self.__sites)
return iter(self.__sites)

def __iter__(self):
return SiteList.Iterator(self.__sites)
return iter(self.__sites)

def __len__(self):
return len(self.__sites)
Expand Down
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import codecs
import sys
from setuptools import setup, find_packages

hubarcode = setup(name='pyGeoDb',
hubarcode = setup(
name='pyGeoDb',
maintainer='Maximillian Dornseif',
maintainer_email='md@hudora.de',
url='http://github.com/mdornseif/pyGeoDb',
version='1.3',
description='distance calculation based on ZIP codes and map generation',
long_description=open('README.rst').read().decode('utf-8'),
long_description=
open('README.rst').read().decode('utf-8') if sys.version < '3'
else open('README.rst', encoding="utf-8").read(),
classifiers=['License :: OSI Approved :: BSD License',
'Intended Audience :: Developers',
'Programming Language :: Python'],
Expand Down
26 changes: 13 additions & 13 deletions tools/createCSV.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
linec += 1
pll = line.split("|")

#map(str, pll)
# map(str, pll)
plz, lon, lat = map(string.strip, pll)

# Feld 1: eindeutiger Schl�ssel (Primary Key)
# Felder 2 bis 8: hierarchische Verwaltungsgliederung, hier:
# Feld 2: Staat (DE == Deutschland)
# Feld 3: Bundesland, s.o.
# Feld 4: Regierungsbezirk
# Feld 5: Landkreis
# Feld 6: Verwaltungszusammenschluss
# Feld 7: Ort
# Felder 8 und 9: Koordinaten:
# Feld 8: L�ngengrad
# Feld 9: Breitengrad
# Feld 10: Postleitzahl
# Feld 1: eindeutiger Schl�ssel (Primary Key)
# Felder 2 bis 8: hierarchische Verwaltungsgliederung, hier:
# Feld 2: Staat (DE == Deutschland)
# Feld 3: Bundesland, s.o.
# Feld 4: Regierungsbezirk
# Feld 5: Landkreis
# Feld 6: Verwaltungszusammenschluss
# Feld 7: Ort
# Felder 8 und 9: Koordinaten:
# Feld 8: L�ngengrad
# Feld 9: Breitengrad
# Feld 10: Postleitzahl

values = ("1", "AT", "", "", "", "", "", lon, lat, plz)

Expand Down
8 changes: 4 additions & 4 deletions tools/draw_germany_borders.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import cairo
import pygeodb
from pprint import pprint
from pygeodb.borderdata import deutschgrenzen
from pygeodb import voronoi


def intRGB(r, g, b):
Expand Down Expand Up @@ -70,7 +72,6 @@ def save(self, fname, vertical):
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
ctx.set_line_join(cairo.LINE_JOIN_ROUND)

from pygeodb.borderdata import deutschgrenzen
geoitems = pygeodb.geodata['de'].items()

# find desired image size
Expand All @@ -95,7 +96,7 @@ def save(self, fname, vertical):
for long, lat in track[1::borderskip]:
ctx.line_to(*ctx.geoscale(long, lat))
ctx.close_path()
#mask_ctx.fill()
# mask_ctx.fill()
ctx.clip()

# draw borders
Expand All @@ -114,7 +115,6 @@ def save(self, fname, vertical):
ctx.stroke()

# calculate voronoi diagram for plz areas
from pygeodb import voronoi
pts = []
for plz, (long, lat, name) in geoitems:
if plz.startswith('422'):
Expand Down Expand Up @@ -165,4 +165,4 @@ def save(self, fname, vertical):

# image background
ctx.show_page()
#ctx.fill()
# ctx.fill()
2 changes: 1 addition & 1 deletion tools/draw_germany_plz.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cairo
import pygeodb
from pprint import pprint
import voronoi


def intRGB(r, g, b):
Expand Down Expand Up @@ -85,7 +86,6 @@ def save(self, fname, vertical):
ctx.close_path()
ctx.stroke()

import voronoi
pts = []
for plz, (long, lat, name) in geoitems:
pts.append(voronoi.Site(long, lat))
Expand Down
4 changes: 2 additions & 2 deletions tools/plz2python.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from pprint import pprint
import sys
import pygeodb.plzdata

geodata = {}

Expand Down Expand Up @@ -33,11 +34,10 @@
country = str(values[1]).upper()
longitude = float(values[7])
latitude = float(values[8])
if not country in geodata:
if country not in geodata:
geodata[country] = {}
geodata[country][zipcode] = (longitude, latitude, city)

import pygeodb.plzdata
geodata.update(pygeodb.plzdata)

outfile = open(sys.argv[2], 'wb')
Expand Down
21 changes: 14 additions & 7 deletions tools/plz2python2013.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@

# parse new style OpenGeoDB data files

from pprint import pprint
import os.path
import sys
from pprint import pformat

import pygeodb.plzdata

geodata = {}

# This assumes input files like DE.tab, AT.tab or CH.tab
country = os.path.splitext(os.path.basename(sys.argv[1]))[0]

for line in open(sys.argv[1], 'rb'):
line = line.decode('utf-8').strip()
if line[0] == "#" or not line:
Expand Down Expand Up @@ -42,22 +48,23 @@
if len(values) < 10:
continue
zipcode = str(values[7])
if len(zipcode) != 5:
if ((country == 'DE' and len(zipcode) != 5) or
(country != 'DE' and len(zipcode) != 4)):
continue
if not values[4]: # keine koordinaten
if not values[4].strip(): # keine koordinaten
continue
city = values[3]
country = 'DE'
longitude = float(values[5])
latitude = float(values[4])
if not country in geodata:
if country not in geodata:
geodata[country] = {}
geodata[country][zipcode] = (longitude, latitude, city)

import pygeodb.plzdata
geodata.update(pygeodb.plzdata.geodata)

outfile = open(sys.argv[2], 'wb')
outfile.write("# -*- coding: utf-8 -*-\n".encode('utf-8'))
outfile.write("# autogenerated - do not edit\n".encode('utf-8'))
outfile.write("from __future__ import unicode_literals\n".encode('utf-8'))
outfile.write("geodata = ".encode('utf-8'))
pprint(geodata, outfile)
outfile.write(pformat(geodata).encode('utf-8'))