def gml_qa(country_code, gml_file): #file read input_gml = utOpen(gml_file) conv_gml = GMLStructure() #fill geometry conv_gml = gml_import(input_gml.read(),conv_gml) if str(country_code).upper() not in COUNTRIES_DICT: country_code = 'EU' country_code = str(country_code).upper() minx = float(COUNTRIES_DICT[country_code]['minx']) miny = float(COUNTRIES_DICT[country_code]['miny']) maxx = float(COUNTRIES_DICT[country_code]['maxx']) maxy = float(COUNTRIES_DICT[country_code]['maxy']) total_number = len(conv_gml.getShp_records()) bad_feaures = 0 mark_bad = 0 if conv_gml.getFeat_type() == '1': for m in range(len(conv_gml.getShp_records())): for n in range (len(conv_gml.getShp_records()[m])): if not isPointInsideRectangle (minx,maxy,maxx,miny,(conv_gml.getShp_records()[m][n])[0][0],(conv_gml.getShp_records()[m][n])[0][1]): mark_bad = 1 if mark_bad: bad_feaures += 1 mark_bad = 0 elif conv_gml.getFeat_type() == '3': for m in range(len(conv_gml.getShp_records())): for k in range (len(conv_gml.getShp_records()[m])): for j in ((conv_gml.getShp_records()[m])[k]): if not isPointInsideRectangle (minx,maxy,maxx,miny,j[0],j[1]): mark_bad = 1 if mark_bad: bad_feaures += 1 mark_bad = 0 elif conv_gml.getFeat_type() == '5': for m in range(len(conv_gml.getShp_records())): for k in range (len(conv_gml.getShp_records()[m])): for j in ((conv_gml.getShp_records()[m])[k]): if not isPointInsideRectangle (minx,maxy,maxx,miny,j[0],j[1]): mark_bad = 1 if mark_bad: bad_feaures += 1 mark_bad = 0 returnQA = QA_TEMPLATE % (total_number,str(total_number-bad_feaures), bad_feaures,("not passed", "passed successfully")[bad_feaures == 0]) return returnQA
def gml_to_kml(gml_file): #generate name name = gml_file[:gml_file.rfind('.')] input_gml = utOpen(gml_file) conv_gml = GMLStructure() conv_gml.setGeo_name(name) # fill geometry conv_gml = gml_import(input_gml.read(),conv_gml) # GML output kml_generator = KMLGenerator() kml_data = kml_generator.fillKMLHeader() kml_data += kml_generator.fillKMLStyle() if conv_gml.getFeat_type() == '1': for m in range(len(conv_gml.getShp_records())): for n in range (len(conv_gml.getShp_records()[m])): x,y = transcalc((conv_gml.getShp_records()[m][n])[0][0],(conv_gml.getShp_records()[m][n])[0][1],10) kml_data += kml_generator.fillKMLPoint("", x, y) elif conv_gml.getFeat_type() == '3': for m in range(len(conv_gml.getShp_records())): thelist = [] temp_thelist = thelist.append for k in range (len(conv_gml.getShp_records()[m])): mylist = [] temp_mylist = mylist.append for j in ((conv_gml.getShp_records()[m])[k]): temp_mylist(transcalc(j[0],j[1],10)) temp_thelist(mylist) kml_data += kml_generator.fillKMLLine("", thelist) elif conv_gml.getFeat_type() == '5': for m in range(len(conv_gml.getShp_records())): thelist = [] temp_thelist = thelist.append for k in range (len(conv_gml.getShp_records()[m])): mylist = [] temp_mylist = mylist.append for j in ((conv_gml.getShp_records()[m])[k]): temp_mylist(transcalc(j[0],j[1],10)) temp_thelist(mylist) kml_data += kml_generator.fillKMLPoly("", thelist) kml_data += kml_generator.fillKMLFooter() return kml_data
def gml_to_image(in_name, in_gml, in_width, in_height, in_filetype, in_out_colour, in_fill_colour, in_country, in_ims_server, in_ims_service, im_background = 0): if im_background: if str(in_country).upper() not in COUNTRIES_DICT: in_country = 'EU' in_country = str(in_country).upper() imsresp = get_background_image(in_ims_server, in_ims_service, COUNTRIES_DICT[in_country]['minx'], COUNTRIES_DICT[in_country]['miny'], COUNTRIES_DICT[in_country]['maxx'], COUNTRIES_DICT[in_country]['maxy'], in_width, in_height) imsresp = download_ims_image(imsresp) imb = Image.open (imsresp.getUrl()) #imb = imb.convert("P") #imb.palette = ImagePalette.ImagePalette("RGB") # make a new empty image im = Image.new("P", (in_width, in_height),255) im.palette = ImagePalette.ImagePalette("RGB") else: # make a new empty image im = Image.new("P", (in_width, in_height),255) im.palette = ImagePalette.ImagePalette("RGB") input_gml = utOpen(in_gml) conv_gml = GMLStructure() conv_gml.setGeo_name(in_name) # fill geometry conv_gml = gml_import(input_gml.read(),conv_gml) # if no geographic data available if len(conv_gml.getShp_records()) == 0: #font = ImageFont.truetype("arial.ttf", 15) font = ImageFont.load_default() draw = ImageDraw.Draw(im) draw.text((10, 10), " NO DATA", font=font, fill = in_out_colour) del draw if conv_gml.getFeat_type() == '1': for m in range(len(conv_gml.getShp_records())): for n in range (len(conv_gml.getShp_records()[m])): draw = ImageDraw.Draw(im) if im_background: x,y = utMapToPoint((conv_gml.getShp_records()[m][n])[0][0],(conv_gml.getShp_records()[m][n])[0][1], in_width, in_height,imsresp.getMinx(),imsresp.getMiny(),imsresp.getMaxx(),imsresp.getMaxy()) else: x,y = utMapToPoint((conv_gml.getShp_records()[m][n])[0][0],(conv_gml.getShp_records()[m][n])[0][1], in_width, in_height,(conv_gml.getXY_min())[0], (conv_gml.getXY_min())[1], (conv_gml.getXY_max())[0], (conv_gml.getXY_max())[1]) draw.rectangle([x-1,y+1,x+1,y-1], outline = in_out_colour) del draw elif conv_gml.getFeat_type() == '3': for m in range(len(conv_gml.getShp_records())): for k in range (len(conv_gml.getShp_records()[m])): mylist = [] temp_mylist = mylist.append for j in ((conv_gml.getShp_records()[m])[k]): if im_background: temp_mylist(utMapToPoint(j[0],j[1], in_width, in_height, imsresp.getMinx(),imsresp.getMiny(),imsresp.getMaxx(),imsresp.getMaxy())) else: temp_mylist(utMapToPoint(j[0],j[1], in_width, in_height,(conv_gml.getXY_min())[0], (conv_gml.getXY_min())[1], (conv_gml.getXY_max())[0], (conv_gml.getXY_max())[1])) draw = ImageDraw.Draw(im) draw.line(mylist, width = 1, fill = in_out_colour) del draw elif conv_gml.getFeat_type() == '5': for m in range(len(conv_gml.getShp_records())): #prepare polygons ordered_shapes = [] temp_ordered_shapes = ordered_shapes.append mypoint = {} pol_has_poi = {} poi_has_pol = {} for k in range (len(conv_gml.getShp_records()[m])): t = (conv_gml.getShp_records()[m])[k][0] mypoint[k] = t for p in range(len(mypoint)): p_count = 0 for k in range (len(conv_gml.getShp_records()[m])): if point_inside_polygon(mypoint[p][0],mypoint[p][1],(conv_gml.getShp_records()[m])[k]) and k <>p: p_count += 1 poi_has_pol[p] = p_count for k in range (len(conv_gml.getShp_records()[m])): p_count = 0 for p in range(len(mypoint)): if point_inside_polygon(mypoint[p][0],mypoint[p][1],(conv_gml.getShp_records()[m])[k]) and k <>p: p_count+=1 pol_has_poi[k] = p_count pol_sort = {} auxlist = [ (value, key) for key, value in pol_has_poi.items() ] auxlist.sort() auxlist.reverse() i = 0 for value, key in auxlist: pol_sort[i] = key i+=1 for k in range (len(conv_gml.getShp_records()[m])): mylist = [] temp_mylist = mylist.append for j in ((conv_gml.getShp_records()[m])[pol_sort[k]]): if im_background: temp_mylist(utMapToPoint(j[0],j[1], in_width, in_height, imsresp.getMinx(),imsresp.getMiny(),imsresp.getMaxx(),imsresp.getMaxy())) else: temp_mylist(utMapToPoint(j[0],j[1], in_width, in_height,(conv_gml.getXY_min())[0], (conv_gml.getXY_min())[1], (conv_gml.getXY_max())[0], (conv_gml.getXY_max())[1])) draw = ImageDraw.Draw(im) if poi_has_pol[pol_sort[k]] % 2 == 0: draw.polygon(mylist, outline = in_out_colour, fill = in_fill_colour) else: draw.polygon(mylist, outline = in_out_colour, fill = 255) del draw if im_background: # cleanup delete_ims_image(imsresp.getUrl()) # save the image file_extension = in_filetype.lower() if in_filetype.lower() == "jpeg": file_extension = "jpg" elif in_filetype.lower() == "TIFF": file_extension = "tif" image_url = join(FILES_PATH, "%s.%s" % (in_name, file_extension)) if im_background: im.save(image_url, "%s" % in_filetype, transparency = 255, color=255) im = Image.open (image_url) im = im.convert("RGBA") imb = imb.convert("RGBA") mask = im.point(lambda i: i > 0 and 200) # use black as transparent imb.paste (im, (0,0), mask) imb.save(image_url, "%s" % in_filetype) else: im.save(image_url, "%s" % in_filetype) l_file = open(image_url, mode='rb') content = l_file.read() l_file.close() delete_ims_image(image_url) return content