2

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?

  • 1
    So you haven't specified what projection you're using, even though you claim there are projection issues, and you haven't provided a picture. Please edit the question to give sufficient details so that one of our volunteers can help you. – Vince Sep 8 '16 at 1:09
  • 2
    Maybe it's not the projection used when generating the hexagons but the projection used when displaying them – Peter Horsbøll Møller Sep 8 '16 at 7:02
5

The result you are seeing is in fact correct.

Looking at your code you create your hexagons in UTM32N but Leaflet displays them using EPSG:3857 (aka Web Mercator) which "stretches" as you go north (or south) from the equator. So your hexagons cover a wider distance (east-west) as you move north making them look stretched.

If you are intending to do some sort of area based calculation using your hexagons you should create your hexagons in an Equal Area projection (based on this question) like EPSG:3035.

If you just want a map of pretty hexagons in Leaflet you should generate them using EPSG:3857 so they won't be distorted when displayed in that projection, however they will be useless for analysis as they will be different sizes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.