def processRequest(environ): path = environ['QUERY_STRING'] p = parse_query(path) if isStr(p): return (p, 'new') mapid = p.get('mapid', 'new') return createImage(path, 'jpg'), mapid
def test(path): outf = createImage(path, 'jpg') if isStr(outf): print(outf) else: fd = open('test.jpg', 'wb') fd.write(outf.read()) fd.close()
def processRequest(req): from mod_python import apache, util path = req.args with open(home + "/logs/oommakerlog-jpg-access.txt", "a") as fa: fa.write( time.strftime('%x %X') + " : " + req.get_remote_host() + " : " + path + "\n") outf = createImage(path, 'jpg') if path.count("|") < 9 or path.count("|") > 10 or len(path) < 30: return "Incorrectly formatted string." if path.count("|") == 9: path = path + "|" style, paper, scale, centre, title, club, mapid, start, crosses, cps, controls = path.split( "|") mapid = mapid.split("=")[1] if isStr(outf): req.status = apache.HTTP_SERVICE_UNAVAILABLE req.content_type = 'text/html' outHTML = "<html><head><title>OpenOrienteeringMap: Error</title></head><body><h1>Error</h1><p>" + outf + "</p></body></html>" req.write(outHTML) with open(home + "/logs/oommakerlog-jpg-error.txt", "a") as fe: fe.write( time.strftime('%x %X') + " : " + req.get_remote_host() + " : " + outf + " : " + path + "\n") return req else: outfsize = os.fstat(outf.fileno()).st_size req.status = apache.HTTP_OK req.content_type = 'image/jpeg' req.headers_out[ "Content-Disposition"] = "attachment; filename=\"oom_" + mapid + ".jpg\"" req.set_content_length(outfsize) req.write(outf.read()) return req
def createKMZ(path): import tempfile import zipfile from handlePDF import createImage from simplekml import Kml, Folder from simplekml.base import Kmlable class PermKml(Kml): def savekmz(self, path, format=True): Kmlable._currentroot = self self._outputkmz = True out = self._genkml(format).encode('utf-8') kmz = zipfile.ZipFile(path, 'w', zipfile.ZIP_DEFLATED) info = zipfile.ZipInfo("doc.kml") info.external_attr = 0100775 << 16 info.date_time = time.localtime() kmz.writestr(info, out) for image in self._images: kmz.write(image, os.path.join('files', os.path.split(image)[1])) for image in self._foundimages: kmz.write(image, os.path.join('files', os.path.split(image)[1])) kmz.close() jpgf = createImage(path, 'jpg', 2) global MAP_NM, MAP_EM, MAP_SM, MAP_WM if path.count("|") < 9 or path.count("|") > 10 or len(path) < 30: return "Incorrectly formatted string." if path.count("|") == 9: path = path + "|" style, paper, scale, centre, title, club, mapid, start, crosses, cps, controls = path.split("|") paper = paper.split("=")[1] PAPER_W = float(paper.split(",")[0]) PAPER_H = float(paper.split(",")[1]) scale = int(scale.split("=")[1]) centre = centre.split("=")[1] clat = int(centre.split(",")[0]) clon = int(centre.split(",")[1]) projection = mapnik.Projection(EPSG900913) wgs84lat = mapnik.Coord(clon, clat).inverse(projection).y scaleCorrectionFactor = math.cos(wgs84lat * math.pi/180) scaleCorrected = scale / scaleCorrectionFactor MAP_W = PAPER_W - MAP_WM - MAP_EM MAP_H = PAPER_H - MAP_NM - MAP_SM paperSLat = clat - (MAP_H/2+MAP_SM)*scaleCorrected paperNLat = clat + (MAP_H/2+MAP_NM)*scaleCorrected paperWLon = clon - (MAP_W/2+MAP_WM)*scaleCorrected paperELon = clon + (MAP_W/2+MAP_EM)*scaleCorrected XMin = clon - (paperELon - paperWLon)/2.0 YMin = clat - (paperNLat - paperSLat)/2.0 XMax = clon + (paperELon - paperWLon)/2.0 YMax = clat + (paperNLat - paperSLat)/2.0 north = mapnik.Coord(XMin, YMax).inverse(projection).y west = mapnik.Coord(XMin, YMax).inverse(projection).x south = mapnik.Coord(XMax, YMin).inverse(projection).y east = mapnik.Coord(XMax, YMin).inverse(projection).x kml = PermKml() kml.document = Folder(name="") jpgfilename = '/tmp/tile_0_0.jpg' jpgfile = open(jpgfilename, 'wb') jpgfile.write(jpgf.read()) jpgfile.flush() jpgfilepath = kml.addfile(jpgfilename) ground = kml.newgroundoverlay(name=os.path.split(jpgfilename)[1]) ground.draworder = 75 ground.icon.href = jpgfilepath ground.icon.viewboundscale = 0.75 ground.latlonbox.north = north ground.latlonbox.south = south ground.latlonbox.east = east ground.latlonbox.west = west ground.latlonbox.rotation = 0 kmzfile = tempfile.NamedTemporaryFile() kml.savekmz(kmzfile.name) return kmzfile
def createKMZ(path): import tempfile import zipfile from handlePDF import createImage from simplekml import Kml, Folder from simplekml.base import Kmlable class PermKml(Kml): def savekmz(self, path, format=True): Kmlable._currentroot = self self._outputkmz = True out = self._genkml(format).encode('utf-8') kmz = zipfile.ZipFile(path, 'w', zipfile.ZIP_DEFLATED) info = zipfile.ZipInfo("doc.kml") info.external_attr = 0o100775 << 16 info.date_time = time.localtime() kmz.writestr(info, out) for image in self._images: kmz.write(image, os.path.join('files', os.path.split(image)[1])) for image in self._foundimages: kmz.write(image, os.path.join('files', os.path.split(image)[1])) kmz.close() jpgf = createImage(path, 'jpg') p = parse_query(path) mapid = p.get('mapid', 'new') global MAP_NM, MAP_EM, MAP_SM, MAP_WM PAPER_W = float(p['paper'].split(",")[0]) PAPER_H = float(p['paper'].split(",")[1]) scale = int(p['scale']) clat = int(p['centre'].split(",")[0]) clon = int(p['centre'].split(",")[1]) try: rotation = float(p['rotation']) except: rotation = 0 projection = mapnik.Projection(EPSG900913) wgs84lat = mapnik.Coord(clon, clat).inverse(projection).y scaleCorrectionFactor = math.cos(wgs84lat * math.pi / 180) scaleCorrected = scale / scaleCorrectionFactor MAP_W = PAPER_W - MAP_WM - MAP_EM MAP_H = PAPER_H - MAP_NM - MAP_SM paperSLat = clat - (MAP_H / 2 + MAP_SM) * scaleCorrected paperNLat = clat + (MAP_H / 2 + MAP_NM) * scaleCorrected paperWLon = clon - (MAP_W / 2 + MAP_WM) * scaleCorrected paperELon = clon + (MAP_W / 2 + MAP_EM) * scaleCorrected XMin = clon - (paperELon - paperWLon) / 2.0 YMin = clat - (paperNLat - paperSLat) / 2.0 XMax = clon + (paperELon - paperWLon) / 2.0 YMax = clat + (paperNLat - paperSLat) / 2.0 TopLeftLat = clat + (MAP_H / 2 + MAP_NM) * scaleCorrected * math.cos( rotation) - (MAP_W / 2 + MAP_WM) * scaleCorrected * math.sin(rotation) TopLeftLon = clon - (MAP_W / 2 + MAP_WM) * scaleCorrected * math.cos( rotation) - (MAP_H / 2 + MAP_NM) * scaleCorrected * math.sin(rotation) north = mapnik.Coord(XMin, YMax).inverse(projection).y west = mapnik.Coord(XMin, YMax).inverse(projection).x south = mapnik.Coord(XMax, YMin).inverse(projection).y east = mapnik.Coord(XMax, YMin).inverse(projection).x kml = PermKml() kml.document = Folder(name="") jpgfilename = '/tmp/tile_0_0.jpg' jpgfile = open(jpgfilename, 'wb') jpgfile.write(jpgf.read()) jpgfile.flush() jpgfilepath = kml.addfile(jpgfilename) ground = kml.newgroundoverlay(name=os.path.split(jpgfilename)[1]) ground.draworder = 75 ground.icon.href = jpgfilepath ground.icon.viewboundscale = 0.75 ground.latlonbox.north = north ground.latlonbox.south = south ground.latlonbox.east = east ground.latlonbox.west = west ground.latlonbox.rotation = rotation * 180 / math.pi kmzfile = tempfile.NamedTemporaryFile() kml.savekmz(kmzfile.name) return kmzfile