def processor(shape): import sys log.openSection("processor at <%s>" % str(__file__), level=log.DEBUG, profile=sys.modules[__name__]) log.debug(shape) log.closeSection()
def processRequest(request, response): log.startCommand() log.openSection("REQUEST:%s::%s" % (request.path, str(request.query))) path = request.path.lower() if path: pathFound = False log.openSection("Searching for path") for operationPath, info in operations.iteritems(): if path.startswith(operationPath.lower()): log.logger.info("Processor for path '%s' found" % operationPath) subPath = request.path[len(operationPath) + 1:] if subPath: request.subPath = subPath.split("/") else: request.subPath = [] processor = PathContainer.getProcessor(info) processor(request, response) pathFound = True if not pathFound: log.logger.info("Unknown request") log.closeSection() if not response.handled: response.buildResult("") log.closeSection()
def __init__(self, svgFileName, charsToBeLoaded=None): self.name = changeFileExtension(extractFileName(svgFileName), "") self.glyphs = {} self.defaults = Container({ "width": 556, "height": None, "x": None, "y": None }) log.openSection("Reading glyphs from %s" % svgFileName, log.DEBUG) svgData = open(svgFileName, "r").read().replace('\n', '') self.letters = u"" startPos = 0 while startPos < len(svgData): startPos = svgData.find('<glyph', startPos) if startPos >= 0: endPos = svgData.find("/>", startPos) + 2 if endPos >= 0: tag = svgData[startPos:endPos] glyph = Glyph(tag, self) if isinstance(glyph.unicode, list): unicodes = glyph.unicode else: unicodes = [glyph.unicode] for unicode in unicodes: if not charsToBeLoaded or unicode in charsToBeLoaded: self.letters += unicode self.glyphs[unicode] = glyph startPos = endPos else: log.error("Unclosed glyph tag at %d" % startPos) break else: break self.letters = "".join(sorted(self.letters)) if charsToBeLoaded and len(charsToBeLoaded) <> len(self.glyphs.keys()): log.openSection("Not all characters found!", log.WARNING) log.warning("%d characters to be loaded, %d found" % (len(charsToBeLoaded), len(self.glyphs.keys()))) log.closeSection(level=log.WARNING) log.closeSection("%d glyphs found" % len(self.glyphs.keys()), level=log.DEBUG)
def getWFSData(databaseName, tableName, bbox, srsName, schemaName="", sqlQuery=""): from shapely.wkb import loads import geojson import pygeotoolbox.sharedtools.log as logger from pygeotoolbox.database import databaseByName from pygeotoolbox.sharedtools import listToSqlStr logger.openSection("Building WFS Data...") FIELD_DEFS = 'graphic_symbol' GEOMETRY_FIELD_NAME = 'geom' """ sampleRequest: http://localhost/cgi-bin/mapserv.exe?map=C:/ms4w/Apache/hidden/MapGen/osm_lund.map&service=wfs&version=1.1.0&request=GetFeature&typename=data_area&outputFormat=application/json&srsname=EPSG:3857&bbox=1471006.5443115234,7501014.372741699,1471356.5870361328,7502471.5505981445,EPSG:3857 """ if not schemaName: schemaName = "mg_work" if __SYMBOLNAMES: symbolNamesWhereClause = "graphic_symbol in %s and " % listToSqlStr(__SYMBOLNAMES) else: symbolNamesWhereClause = "" logger.info("Symbol names:" + str(listToSqlStr(__SYMBOLNAMES))) logger.info("bbox:" + str(bbox)) sql = "select %s, (st_dump(%s)).geom from %s.%s, (select ST_MakeEnvelope(%s) as envelope) as bbox where %s %s && bbox.envelope" % (FIELD_DEFS, GEOMETRY_FIELD_NAME, schemaName, tableName, bbox, symbolNamesWhereClause, GEOMETRY_FIELD_NAME) if sqlQuery: sql += " and (%s)" % sqlQuery fieldNames = FIELD_DEFS.split(',') logger.info("Loading features...") features = [] database = databaseByName(databaseName) for row in database.executeSelectSQL(sql): shape = loads(row[len(row) - 1], hex=True) properties = {} for fieldName, fieldIndex in zip(fieldNames, range(len(fieldNames))): properties[fieldName] = row[fieldIndex] if __DUMP_SYMBOLS_PROC: features.extend(__DUMP_SYMBOLS_PROC(shape, properties)) else: features.append(geojson.Feature(geometry=shape, properties=properties)) logger.info("%d features found" % len(features)) logger.closeSection() return '{ "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::%s" } }, ' % srsName + geojson.dumps(geojson.FeatureCollection(features))[1:]
def readTestFonts(): import os fonts = [] for fontName in ['ArialMT.svg', 'Helvetica-Regular.svg']: log.openSection("Loading font %s into database..." % fontName) font = SVGFontReader( os.path.dirname(__file__) + os.sep + fontName, addCharaters(getLowerASCII(), u'ěščřžýáíéúůĚŠČŘŽÝÁÍÉÚŮ')) fonts.append(font) log.info("'%s':%d glyphs readed." % (fontName, len(font.glyphs.keys()))) log.info("[%s]" % font.letters) log.closeSection() return fonts
def provideFileProcessor(self, request, response): log.openSection("Processing path alias...") log.debug("request.subPath=" + str(request.subPath)) fileNameBase = "/".join(request.subPath) if fileNameBase: log.debug("Requesting file '%s'" % fileNameBase) fileName = os.path.normpath(os.path.join(self.path, fileNameBase)) if os.path.exists(fileName): if os.path.isfile(fileName): response.htmlData = self.applyTemplates(fileName) response.mimeFormat = getMimeType(fileName) response.handled = True else: log.error(fileName + " is a directory.") response.msgs.append(fileName + " is a directory.") else: log.error("File '%s' not found" % fileNameBase) response.msgs.append("File %s not found" % fileNameBase) else: log.error("You must specify file name") log.closeSection()
def createDatabase(self, databaseName): """ :param databaseName: """ import sys from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT log.openSection("Creating database '%s'" % config.database.name, level=log.DEBUG) sql = "create database %s;" % databaseName log.debug(sql) connection = psycopg2.connect(user=config.database.user, host = config.database.host, password=config.database.password) connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) cursor = connection.cursor() cursor.execute(sql) cursor.close() connection.close() if config.database.onCreateDatabaseSQL: log.debug(config.database.onCreateDatabaseSQL) self.execute(config.database.onCreateDatabaseSQL) log.closeSection()
def __init__(self, templateFileName): from pygeotoolbox.sharedtools import fileToStr, log log.openSection("Reading HTML Templates from '%s'" % templateFileName) self.templates = {} templatesContent = fileToStr(templateFileName) searchPos = 0 while True: templateEndMarker_StartPos = templatesContent.find("<!-- END OF ", searchPos) if templateEndMarker_StartPos >= 0: templateEndMarker_EndPos = templatesContent.find("-->", templateEndMarker_StartPos) templateName = templatesContent[templateEndMarker_StartPos+12:templateEndMarker_EndPos].strip() templateStartMarker_StartPos = templatesContent.find("<!-- %s -->" % templateName, searchPos) if templateStartMarker_StartPos>=0: templateContent = templatesContent[templateStartMarker_StartPos+len(templateName)+9:templateEndMarker_StartPos] self.templates[templateName] = templateContent else: log.error("'%s' not found" % "<!-- %s -->" % templateName) searchPos = templateEndMarker_EndPos + 3 else: break log.info("Template names:%s (HTMLTemplateProcessor)" % str(self.templates.keys())) log.closeSection()
if __name__ == "__main__": from shapely.wkt import loads from shapely.geometry import Point, Polygon, LineString from point import processor as pointProcessor from linestring import processor as linestringProcessor from polygon import processor as polygonProcessor import pygeotoolbox.sharedtools.log as log processors = { Point: pointProcessor, Polygon: polygonProcessor, LineString: linestringProcessor } import point log.LOGGED_PROFILES = [point] database = Database() cursor = database.cursor() cursor.query("select id, name, geom from public.roads limit 13;") log.openSection("Reading database rows", level=log.DEBUG) for row in cursor: log.openSection("Processing row:%s" % str(row), level=log.DEBUG) shape = loads(row[2]) if shape.__class__ in processors: processor = processors[shape.__class__] processor(shape) # else: element is skipped log.closeSection() log.closeSection()
if isinstance(svgReaderOrSVGFileName, SVGFontReader): svgFontReader = svgReaderOrSVGFileName else: svgFontReader = SVGFontReader(svgReaderOrSVGFileName, charsToBeLoaded=None) self.glyphs = svgFontReader.glyphs self.name = svgFontReader.name if saveToDatabase: self.saveToDatabase() if __name__ == "__main__": from svg.svgfontreader import readTestFonts log.openSection("Reading svg fonts...") fonts = readTestFonts() log.closeSection() log.openSection("Saving fonts to database...") for svgFont in readTestFonts(): log.info(svgFont.name) font = Font("") font.readFromSVG(svgFont) log.closeSection() log.closeSection() # @TODO Některé základní znaky se načtou, ale nemají glyph # @TODO Sloupce unicode a letters se nenaplňují # @TODO Potřeba vytvořit algoritmus pro sestavení textu podél křivky či čáry, s volbou text originu, případných offsetů atd.
def processor(shape): log.openSection("processor at <%s>" % str(__file__), level=log.DEBUG) log.debug(shape) log.closeSection()