We are trying to generate a grid of hexagons that is supposed to span the whole of Germany. The code we are using to generate this grid is as follows (excluding some sqlalchemy related parts):
from shapely.geometry import Polygon, Point
from pyproj import Proj, transform
class HexGenImpl(object):
def __init__(self, size):
self.size = size
@property
def width(self):
return self.size * 2.0
@property
def height(self):
return self.width * ( math.sqrt(3) / 2.0 )
def colFactor(self, col):
return (col * self.width * (3.0 / 4.0))
def rowFactor(self, row):
return (row * self.height)
def __call__(self, col, row):
for angle in range(0, 360+60, 60):
theta = math.radians(angle)
x = self.colFactor(col)
y = self.rowFactor(row)
# Offsetting every other column to fit the grid
y += (col & 1) * 0.5 * self.height
x += math.cos(theta) * self.size
y += math.sin(theta) * self.size
x = round(x, 10)
y = round(y, 10)
if x == -0: x = 0
if y == -0: y = 0
yield (x, y)
def generateHexGrid():
session = Session()
hexagon_generator = HexGenImpl(.1)
ap = Proj("+proj=utm +zone=32 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")
# Formerly just Proj("+proj=EPSG:4326")
for a in range(100):
for b in range(400):
hexagon = hexagon_generator(a, b)
hList = list(hexagon)
glist = [ap(p[0], p[1]) for p in hList]
pol = Polygon([[p[0], p[1]] for p in glist])
tHex = Hexagon(geom=pol.wkt)
session.add(tHex)
print(a)
session.commit()
This code works in that it generates a grid that can be rendered using our Geoserver without any problems. Sadly, whenever we try to use the layer in our front-end-application (which uses Leaflet) the hexagons appear stretched. This seems to be a problem with the projection, yet I haven't been able to figure out how one would go about projecting our coordinates (which are basically points on a cartesian plane) to something that gets rendered properly. The closer the hexagons are to the equator, the less they appear to be stretched out.
I tried multiple projections, offsetting the coordinates, changing settings on the GeoServer etc. yet the problem persists.
How do I go about this?