from shapely.geometry import box

from c3nav.mapdata.render.base import get_render_level_data
from c3nav.mapdata.utils.svg import SVGImage


def render_svg(level, miny, minx, maxy, maxx, scale=1):
    svg = SVGImage(bounds=((miny, minx), (maxy, maxx)), scale=scale, buffer=2)

    within_coords = (minx-2, miny-2, maxx+2, maxy+2)
    bbox = box(*within_coords)

    render_level_data = get_render_level_data(level)

    crop_to = None
    primary_level_count = 0
    for geoms, default_height in reversed(render_level_data):
        if geoms.holes is not None:
            print(geoms.holes.area)
            primary_level_count += 1

        geoms.crop_to = crop_to if primary_level_count > 1 else None

        if geoms.holes is not None:
            if crop_to is None:
                crop_to = geoms.holes
            else:
                crop_to = crop_to.intersection(geoms.holes)

    for geoms, default_height in render_level_data:
        crop_to = bbox
        if geoms.crop_to is not None:
            crop_to = crop_to.intersection(geoms.crop_to)

        for altitudearea_geom, altitude in geoms.altitudeareas:
            svg.add_geometry(crop_to.intersection(altitudearea_geom), fill_color='#eeeeee', altitude=altitude)

        svg.add_geometry(crop_to.intersection(geoms.walls),
                         fill_color='#aaaaaa', stroke_px=0.5, stroke_color='#aaaaaa', elevation=default_height)

        svg.add_geometry(crop_to.intersection(geoms.doors), fill_color='#ffffff', elevation=0)

    return svg

@@ -266,17 +266,17 @@
  buffer=
-2
+1
 )%0A%0A    w
@@ -300,33 +300,33 @@
 inx-
-2
+1
 , miny-
-2
+1
 , maxx+
-2
+1
 , maxy+
-2
+1
 )%0A  

reduce buffer to 1m