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_shp(gml_file, schema_file, user_enc="utf-8"): #generate name name_tmp = os.path.split(gml_file)[1] if '.' in name_tmp: name = name_tmp[:name_tmp.rfind('.')] else: name = name_tmp #file read input_gml = utOpen(gml_file) if not schema_file: schema_file = XSD_FILE input_schema = utOpen(schema_file) conv_gml = GMLStructure() #fill schema(dbf) information try: conv_gml = gml_sd_import(input_schema.read()) except Exception, strerror: return "", strerror
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
else: recdbf_toadd[ntag] = int(valtag) elif dbf_recc_type[ntag] == 'decimal': if valtag == 'None' or valtag == '': recdbf_toadd[ntag] = float() else: recdbf_toadd[ntag] = float(valtag) elif dbf_recc_type[ntag] == 'string': recdbf_toadd[ntag] = str(valtag) dbf.write_record(k, recdbf_toadd) recdbf_toadd.clear dbf.close() #Fill metadata gml_generator = GMLGenerator(user_enc) input_metadata = utOpen(gml_file) retMetadata = meta_import(input_metadata.read()) l_file = open(os.path.join(FILES_PATH, '%s%s' % (name, '.xml')), 'wb') l_file.write(gml_generator.fillExportMetadata(retMetadata, name)) l_file.close() #Fill projection input_prj = utOpen(gml_file) retProjection = prj_import(input_prj.read()) l_file = open(os.path.join(FILES_PATH, '%s%s' % (name, '.prj')), 'wb') l_file.write(str(retProjection)) l_file.close() res = zip_generator(FILES_PATH, name)
def shp_to_gml(filename, in_schema, user_enc="utf-8", temp_name=None): #generate name name_tmp = os.path.split(filename)[1] if '.' in name_tmp: name = name_tmp[:name_tmp.rfind('.')] else: name = name_tmp if temp_name: meta_filename = temp_name else: meta_filename = name # Creates the GML file inputfilepath = os.path.join(FILES_PATH, filename) shp = shapelib.ShapeFile(inputfilepath) dbf = dbflib.DBFFile(inputfilepath) # GML output gml_generator = GMLGenerator(user_enc) # Some alias shp_read = shp.read_object #file read if not in_schema: in_schema = XSD_FILE input_schema = utOpen(in_schema) # Fill schema(dbf) information conv_gml = gml_sd_import(input_schema.read()) #Read metadata retMetadata = {} try: input_metadata = utOpen("%s.xml" % os.path.join(FILES_PATH, name)) retMetadata = meta_import(input_metadata.read()) except: for k in METADATA_LABELS: retMetadata[k] = '' #Read projection try: input_prj = utOpen("%s.prj" % os.path.join(FILES_PATH, name)).read() except: input_prj = '' dbf_filter = {} for j in range(len(conv_gml.getRec_dbf())): ttype, tname, tlen, tdecc = (conv_gml.getRec_dbf())[j] dbf_filter[tname] = {'type':ttype, 'len':tlen, 'decc':tdecc} gml_data = gml_generator.fillHeader(in_schema) gml_data += gml_generator.fillMetadata(retMetadata, meta_filename) gml_data += gml_generator.fillProjection(input_prj) gml_data += gml_generator.fillBoundingBox(shp.info()[2][0], shp.info()[2][1], shp.info()[3][0], shp.info()[3][1]) for j in range(dbf.record_count()): dbf_ready = {} for k,v in ((dbf.read_record(j)).items()): if k in dbf_filter: if dbf_filter[k]['type'] == 'string': if len(v) is not 0: if len(v) > int(dbf_filter[k]['len']): v = v[:int(dbf_filter[k]['len'])] if dbf_filter[k]['type'] == 'decimal': if not len(str(v)) == 0: if not '.' in str(v): v = float(str(v) + ".0") whole,decimal = str(v).split('.') if len(whole) > int(dbf_filter[k]['len']): whole = whole[:int(dbf_filter[k]['len'])] if len(decimal) > int(dbf_filter[k]['decc']): decimal = decimal[:int(dbf_filter[k]['decc'])] v = whole + '.' + decimal if dbf_filter[k]['type'] == 'integer': if not len(v) == 0: if len(v) > int(dbf_filter[k]['len']): v = v[:int(dbf_filter[k]['len'])] dbf_ready[k] = v # Add rest of the records as null for kf in dbf_filter: if kf not in dbf_ready: dbf_ready[kf] = '' # Geometry types # Point if shp.info()[1] == 1: gml_data += gml_generator.fillFeatureMemberPoint("F%s" % j, shp_read(j).vertices()[0][0], shp_read(j).vertices()[0][1], dbf_ready) # Polyline elif shp.info()[1] == 3: gml_data += gml_generator.fillFeatureMemberLine("F%s" % j, shp_read(j).vertices(), dbf_ready) # Polygon elif shp.info()[1] == 5: gml_data += gml_generator.fillFeatureMemberPolygon("F%s" % j, shp_read(j).vertices(), dbf_ready) gml_data += gml_generator.fillFooter() shp.close() dbf.close() return gml_data