''' From the points that we got from spiro_pointgen.js, we store the points in a CSV file and then run this script to generate the KML file ''' from simplekml import Kml, Style import csv inputfile = csv.reader(open('spirograph_points.csv', 'r')) kml = Kml() sharedstyle = Style() sharedstyle.labelstyle.color = '7dff0000' #Blue for row in inputfile: pnt = kml.newpoint(name="Point", coords=[(row[0], row[1])]) pnt.style = sharedstyle kml.save('spiro.kml')
# Close table html_str += "</table>\n" return html_str # The model is defined from 89.5 to -89.5 latitude and -179.5 to 179.5 # longitude in 1 degree increments, we'll work with each cell lats = np.arange(89.5, -90, -1) lons = np.arange(-179.5, 180, 1) # Make model instance model = crust1.crustModel() # Use a style for the points, this will save space in the kml file style = Style() style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/pal4/icon49.png' style.labelstyle.scale = 0 # Run the model for each lat and lon pair print "Running model worldwide..." for lon in lons: for lat in lats: model_results = model.get_point(lat, lon) result_html = convert_to_html(model_results) pnt = fol.newpoint() pnt.name = "CRUST 1.0 (%.1f,%.1f)" % (lat, lon) pnt.description = result_html pnt.coords = [(lon, lat)] pnt.style = style
def run_module(mod_name,query_name,database_names,activity,key_timestamp,sql_query): global records global total_loc_records for db in database_names: print("\tExecuting module on: " + db) if args.k == True and activity == "Location": kml = Kml() sharedstyle = Style() sharedstyle.iconstyle.color = 'ff0000ff' conn = sqlite3.connect(db) with conn: conn.row_factory = sqlite3.Row cur = conn.cursor() try: sql = sql_query cur.execute(sql) rows = cur.fetchall() num_records = str(len(rows)) print("\t\tNumber of Records: " + num_records) records = records + len(rows) headers = [] for x in cur.description: headers.append(x[0]) loc_records = 0 for row in rows: col_row = OrderedDict() col_row = (OrderedDict(list(zip(headers,row)))) data_stuff = "" for k,v in six.iteritems(col_row): data = "[" + str(k) + ": " + str(v) + "] " try: data_stuff = data_stuff + data except: data_stuff = [x for x in data_stuff if x in string.printable] data_stuff = data_stuff + data if args.o == 'csv': key = col_row[key_timestamp] try: loccsv.writerow([key,activity, data_stuff,db,mod_name]) except: loccsv.writerow([key,activity, data_stuff.encode('utf8'),db,mod_name]) elif args.o == 'sql': key = col_row[key_timestamp] cw.execute("INSERT INTO APOLLO (Key, Activity, Output, Database, Module) VALUES (?, ?, ?, ?, ?)",(key, activity, data_stuff, db, mod_name)) if len(rows) > 0: if args.k == True and activity == "Location": coords_search = re.search(r'COORDINATES: [\d\.\,\ \-]*',data_stuff) coords = coords_search.group(0).split(" ") point = kml.newpoint(name=key) point.description = ("Data: " + data_stuff) point.timestamp.when = key point.style = sharedstyle point.coords = [(coords[2],coords[1])] loc_records = loc_records + 1 total_loc_records = total_loc_records + 1 if len(rows) > 0: if args.k == True and activity == "Location": kmzfilename = query_name + ".kmz" print("\t\tNumber of Location Records: " + str(loc_records)) print("\t\t===> Saving KMZ to " + kmzfilename + "...") kml.savekmz(kmzfilename) except: print("\t***ERROR***: Could not parse database ["+ db +"]. Often this is due to file permissions, or changes in the database schema. This also happens with same-named databases that contain different data (ie: cache_encryptedB.db). Try using chown/chmod to change permissions/ownership.")
def graph_kml( env, fname="graph.kml", icon="http://maps.google.com/mapfiles/kml/shapes/donut.png", size=0.5, scale=0.5, width=5, ): """Create a kml visualisation of graph. Env variable needs to contain graph.""" # create a kml file containing the visualisation kml = Kml() fol = kml.newfolder(name="Vessels") shared_style = Style() shared_style.labelstyle.color = "ffffffff" # White shared_style.labelstyle.scale = size shared_style.iconstyle.color = "ffffffff" # White shared_style.iconstyle.scale = scale shared_style.iconstyle.icon.href = icon shared_style.linestyle.color = "ff0055ff" # Red shared_style.linestyle.width = width nodes = list(env.FG.nodes) # each timestep will be represented as a single point for log_index, _ in enumerate(list(env.FG.nodes)[0:-1 - 1]): pnt = fol.newpoint( name="", coords=[( nx.get_node_attributes(env.FG, "Geometry")[nodes[log_index]].x, nx.get_node_attributes(env.FG, "Geometry")[nodes[log_index]].y, )], ) pnt.style = shared_style edges = list(env.FG.edges) for log_index, _ in enumerate(list(env.FG.edges)[0:-1 - 1]): lne = fol.newlinestring( name="", coords=[ ( nx.get_node_attributes(env.FG, "Geometry")[edges[log_index][0]].x, nx.get_node_attributes(env.FG, "Geometry")[edges[log_index][0]].y, ), ( nx.get_node_attributes(env.FG, "Geometry")[edges[log_index][1]].x, nx.get_node_attributes(env.FG, "Geometry")[edges[log_index][1]].y, ), ], ) lne.style = shared_style kml.save(fname)
from scripts.models import AnsibleScript, ClassicScript from simplekml import Color, Kml, Style from subprocess import Popen from tasks.models import (AnsibleTask, NapalmConfigTask, NapalmGettersTask, NetmikoTask) blueprint = Blueprint('views_blueprint', __name__, url_prefix='/views', template_folder='templates', static_folder='static') styles = {} for subtype in node_subtypes: point_style = Style() point_style.labelstyle.color = Color.blue path_icon = join(blueprint.root_path, 'static', 'images', 'default', '{}.gif'.format(subtype)) point_style.iconstyle.icon.href = path_icon styles[subtype] = point_style for subtype, cls in link_class.items(): line_style = Style() # we convert the RGB color to a KML color, # i.e #RRGGBB to #AABBGGRR kml_color = "#ff" + cls.color[-2:] + cls.color[3:5] + cls.color[1:3] line_style.linestyle.color = kml_color styles[subtype] = line_style
def site_kml( env, sites, fname="site_development.kml", icon="http://maps.google.com/mapfiles/kml/shapes/square.png", size=1, scale=3, stepsize=120, ): """Create a kml visualisation of vessels. Env variable needs to contain epoch to enable conversion of simulation time to real time. Vessels need logs that contain geometries in lat, lon as a function of time.""" # create a kml file containing the visualisation kml = Kml() fol = kml.newfolder(name="Sites") # each timestep will be represented as a single point for site in sites: for log_index, value in enumerate(site.log["Timestamp"][:-1]): style = Style() style.labelstyle.color = "ffffffff" # White style.labelstyle.scale = 1 style.iconstyle.color = "ff00ffff" # Yellow style.iconstyle.scale = scale * (site.log["Value"][log_index] / site.container.capacity) style.iconstyle.icon.href = icon begin = site.log["Timestamp"][log_index] end = site.log["Timestamp"][log_index + 1] pnt = fol.newpoint( name=site.name, coords=[( site.log["Geometry"][log_index].x, site.log["Geometry"][log_index].y, )], ) pnt.timespan.begin = begin.isoformat() pnt.timespan.end = end.isoformat() pnt.style = style # include last point as well style = Style() style.labelstyle.color = "ffffffff" # White style.labelstyle.scale = 1 style.iconstyle.color = "ff00ffff" # Yellow style.iconstyle.scale = scale * (site.log["Value"][log_index + 1] / site.container.capacity) style.iconstyle.icon.href = icon begin = site.log["Timestamp"][log_index + 1] # end = site.log["Timestamp"][log_index + 1] pnt = fol.newpoint( name=site.name, coords=[( site.log["Geometry"][log_index + 1].x, site.log["Geometry"][log_index + 1].y, )], ) pnt.timespan.begin = begin.isoformat() # pnt.timespan.end = end.isoformat() pnt.style = style kml.save(fname)
def vessel_kml( env, vessels, fname="vessel_movements.kml", icon="http://maps.google.com/mapfiles/kml/shapes/sailing.png", size=1, scale=1, stepsize=120, ): """Create a kml visualisation of vessels. Env variable needs to contain epoch to enable conversion of simulation time to real time. Vessels need logs that contain geometries in lat, lon as a function of time.""" # create a kml file containing the visualisation kml = Kml() fol = kml.newfolder(name="Vessels") shared_style = Style() shared_style.labelstyle.color = "ffffffff" # White shared_style.labelstyle.scale = size shared_style.iconstyle.color = "ffff0000" # Blue shared_style.iconstyle.scale = scale shared_style.iconstyle.icon.href = icon # each timestep will be represented as a single point for vessel in vessels: geom_x = [] geom_y = [] for geom in vessel.log["Geometry"]: geom_x.append(geom.x) geom_y.append(geom.y) vessel.log["Geometry - x"] = geom_x vessel.log["Geometry - y"] = geom_y time_stamp_min = min(vessel.log["Timestamp"]).timestamp() time_stamp_max = max(vessel.log["Timestamp"]).timestamp() steps = int(np.floor((time_stamp_max - time_stamp_min) / stepsize)) timestamps_t = np.linspace(time_stamp_min, time_stamp_max, steps) times = [] for t in vessel.log["Timestamp"]: times.append(t.timestamp()) vessel.log["timestamps_t"] = timestamps_t vessel.log["timestamps_x"] = np.interp(timestamps_t, times, vessel.log["Geometry - x"]) vessel.log["timestamps_y"] = np.interp(timestamps_t, times, vessel.log["Geometry - y"]) for log_index, value in enumerate(vessel.log["timestamps_t"][:-1]): begin = datetime.datetime.fromtimestamp( vessel.log["timestamps_t"][log_index]) end = datetime.datetime.fromtimestamp( vessel.log["timestamps_t"][log_index + 1]) pnt = fol.newpoint( name=vessel.name, coords=[( vessel.log["timestamps_x"][log_index], vessel.log["timestamps_y"][log_index], )], ) pnt.timespan.begin = begin.isoformat() pnt.timespan.end = end.isoformat() pnt.style = shared_style # include last point as well begin = datetime.datetime.fromtimestamp( vessel.log["timestamps_t"][log_index + 1]) # end = datetime.datetime.fromtimestamp(vessel.log["timestamps_t"][log_index + 1]) pnt = fol.newpoint( name=vessel.name, coords=[( vessel.log["timestamps_x"][log_index + 1], vessel.log["timestamps_y"][log_index + 1], )], ) pnt.timespan.begin = begin.isoformat() # pnt.timespan.end = end.isoformat() pnt.style = shared_style kml.save(fname)
def generate_graph(input_file, output_file): carers, visits = load_solution(input_file) #write corresponding KLM file for display in Google Earth kml = Kml() #create palette of colors for carers and sorting carers visits colors = generate_colors(len(carers)) i = 0 for carer in carers: #setting style if not strtobool(carer.dropped): carer_style = Style() carer_style.iconstyle.color = colors[i] carer_style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/man.png' carer_style.linestyle.color = colors[i] carer_style.linestyle.width = 5 carer.set_style(carer_style) i = i + 1 # carers pins + info for carer in carers: if not strtobool(carer.dropped): carer.sort_visits() pnt = kml.newpoint(coords=[(float(carer.visits[0].lon), float(carer.visits[0].lat))]) pnt.extendeddata.newdata(name='sap_number', value=carer.sap_number, displayname="SAP number") pnt.extendeddata.newdata(name='work_relative', value=carer.work_relative, displayname="Relative working time") pnt.extendeddata.newdata(name='work_total_time', value=carer.work_total_time, displayname="Total working time") pnt.extendeddata.newdata(name='work_available_time', value=carer.work_available_time, displayname="Available working time") pnt.extendeddata.newdata(name='work_service_time', value=carer.work_service_time, displayname="Service working time") pnt.extendeddata.newdata(name='work_travel_time', value=carer.work_travel_time, displayname="Travelling time") pnt.extendeddata.newdata(name='work_idle_time', value=carer.work_idle_time, displayname="Idle time") pnt.extendeddata.newdata(name='work_visits_count', value=carer.work_visits_count, displayname="Number of visits") if len(carer.visits) > 0: pnt.style.iconstyle = carer.style.iconstyle pnt.timespan.begin = datetime.datetime.strptime( carer.visits[0].start_time, '%Y-%b-%d %H:%M:%S').isoformat() pnt.timespan.end = datetime.datetime.strptime( carer.get_endtime(), '%Y-%b-%d %H:%M:%S').isoformat() # visits pins + info for visit in visits: pnt = kml.newpoint(coords=[(float(visit.lon), float(visit.lat))]) pnt.extendeddata.newdata(name='user', value=visit.user, displayname="User ID") pnt.extendeddata.newdata(name='start_time', value=visit.start_time, displayname="Start time") pnt.extendeddata.newdata(name='duration', value=visit.duration, displayname="Duration") if visit.is_multiple_carers(): pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/paddle/wht-blank.png' else: pnt.style.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/pushpin/wht-pushpin.png' if not strtobool(visit.dropped): pnt.extendeddata.newdata(name='assigned_carer', value=visit.assigned_carer, displayname="Assigned carer SAP") pnt.extendeddata.newdata(name='satisfaction', value=visit.satisfaction, displayname="User satisfaction") if not strtobool(visit.dropped): for carer in carers: if visit.assigned_carer == carer.sap_number: carer_style = carer.style end_time = carer.get_endtime() break if carer_style: pnt.style.iconstyle.color = carer_style.iconstyle.color pnt.timespan.begin = datetime.datetime.strptime( visit.start_time, '%Y-%b-%d %H:%M:%S').isoformat() pnt.timespan.end = datetime.datetime.strptime( end_time, '%Y-%b-%d %H:%M:%S').isoformat() else: #if viist is dropped pnt.style.iconstyle.color = Color.rgb(0, 0, 0) # adding edges for carer in carers: if not strtobool(carer.dropped): if len(carer.visits) > 1: for i in range(0, len(carer.visits) - 1): source = carer.visits[i] target = carer.visits[i + 1] linestring = kml.newlinestring() linestring.coords = [ (float(source.lon), float(source.lat), 0), (float(target.lon), float(target.lat), 0) ] linestring.style.linestyle = carer.style.linestyle linestring.timespan.begin = datetime.datetime.strptime( target.start_time, '%Y-%b-%d %H:%M:%S').isoformat() linestring.timespan.end = datetime.datetime.strptime( carer.get_endtime(), '%Y-%b-%d %H:%M:%S').isoformat() kml.save(output_file)
def export(request, ref): mosaic_obj = Mosaic.objects.get(ref=ref) if not mosaic_obj.big_preview_url: imgByteArr = mosaic_obj.generatePreview(100) response = cloudinary.uploader.upload(imgByteArr, public_id=mosaic_obj.ref + '_100') mosaic_obj.big_preview_url = response['url'] mosaic_obj.save() kml = Kml() normalstyle = Style() normalstyle.iconstyle.color = 'ffd18802' normalstyle.iconstyle.scale = 1 normalstyle.iconstyle.icon.href = 'http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png' normalstyle.iconstyle.hotspot.x = 32 normalstyle.iconstyle.hotspot.xunits = 'pixels' normalstyle.iconstyle.hotspot.y = 64 normalstyle.iconstyle.hotspot.xunits = 'insetPixels' normalstyle.labelstyle.scale = 0 normalstyle.linestyle.color = 'ffd18802' normalstyle.linestyle.width = 5 highlightstyle = Style() highlightstyle.iconstyle.color = 'ffd18802' highlightstyle.iconstyle.scale = 1 highlightstyle.iconstyle.icon.href = 'http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png' highlightstyle.iconstyle.hotspot.x = 32 highlightstyle.iconstyle.hotspot.xunits = 'pixels' highlightstyle.iconstyle.hotspot.y = 64 highlightstyle.iconstyle.hotspot.xunits = 'insetPixels' highlightstyle.labelstyle.scale = 1 stylemap = StyleMap(normalstyle, highlightstyle) folder = kml.newfolder(name=mosaic_obj.title) for mission_obj in mosaic_obj.missions.all().order_by('order'): multipnt = folder.newmultigeometry(name=mission_obj.title) multipnt.stylemap = stylemap multipnt.description = '<![CDATA[<img src="' + mosaic_obj.big_preview_url + '" height="200" width="auto" />' + mission_obj.desc + ']]>' multipnt.extendeddata.newdata('MIM link', 'https://www.myingressmosaics.com/mosaic/' + mosaic_obj.ref) pnt = multipnt.newpoint() linestring = multipnt.newlinestring(name=mission_obj.title) actions = '' coordinates = [] jsondata = json.loads(mission_obj.data) if len(jsondata) > 9: index = 0 for portal in jsondata[9]: index += 1 lat = 0.0 lng = 0.0 if portal[5]: if portal[5][0] == 'f': lat = portal[5][1] / 1000000.0 lng = portal[5][2] / 1000000.0 if portal[5][0] == 'p': lat = portal[5][2] / 1000000.0 lng = portal[5][3] / 1000000.0 actions += str(index) + '.' if portal[2] == 'Unavailable': actions += 'Unavailable ' else: if portal[4] == 1: actions += 'hack ' if portal[4] == 2: actions += 'capture ' if portal[4] == 7: actions += 'view ' if portal[4] == 8: actions += 'passphrase ' if lat and lng: coordinates.append((lng, lat)) if lat and lng and index==1: pnt.coords = [(lng, lat)] linestring.coords = coordinates multipnt.extendeddata.newdata('actions', actions) response = HttpResponse(kml.kml()) response['Content-Disposition'] = 'attachment; filename="' + mosaic_obj.title + '.kml"' response['Content-Type'] = 'application/kml' return response
def run_module(mod_name, query_name, database_names, activity, key_timestamp, sql_query): global records global total_loc_records for db in database_names: print("\tExecuting module on: " + db) if args.k == True and "Location" in activity: kml = Kml() sharedstyle = Style() sharedstyle.iconstyle.color = 'ff0000ff' conn = sqlite3.connect(db) with conn: conn.row_factory = sqlite3.Row cur = conn.cursor() try: try: sql = sql_query cur.execute(sql) except: print( "\tSQL Query not supported for this version of the database." ) try: rows = cur.fetchall() except: print("\t\tERROR: Cannot fetch query contents.") num_records = str(len(rows)) print("\t\tNumber of Records: " + num_records) records = records + len(rows) headers = [] for x in cur.description: headers.append(x[0]) loc_records = 0 for row in rows: col_row = OrderedDict() col_row = (OrderedDict(list(zip(headers, row)))) data_stuff = "" for k, v in iter(col_row.items()): data = "[" + str(k) + ": " + str(v) + "] " try: data_stuff = data_stuff + data except: data_stuff = [ x for x in data_stuff if x in string.printable ] data_stuff = data_stuff + data if output == 'csv': key = col_row[key_timestamp] if "\n" in data_stuff: data_stuff = data_stuff.replace('\n', "<nl>") if "\r" in data_stuff: data_stuff = data_stuff.replace('\r', "<cr>") try: loccsv.writerow( [key, activity, data_stuff, db, mod_name]) except: loccsv.writerow([ key, activity, data_stuff.encode('utf8'), db, mod_name ]) elif output == 'sql': key = col_row[key_timestamp] cw.execute( "INSERT INTO APOLLO (Key, Activity, Output, Database, Module) VALUES (?, ?, ?, ?, ?)", (key, activity, data_stuff, db, mod_name)) elif output == 'sql_json': key = col_row[key_timestamp] cw.execute( "INSERT INTO APOLLO (Key, Activity, Output, Database, Module) VALUES (?, ?, ?, ?, ?)", (key, activity, json.dumps(col_row, indent=4), db, mod_name)) if len(rows) > 0: if args.k == True and "COORDINATES" in data_stuff: coords_search = re.search( r'COORDINATES: [\d\.\,\ \-]*', data_stuff) coords = coords_search.group(0).split(" ") point = kml.newpoint(name=key) point.description = ("Data: " + data_stuff) point.timestamp.when = key point.style = sharedstyle point.coords = [(coords[2], coords[1])] loc_records = loc_records + 1 total_loc_records = total_loc_records + 1 if loc_records: kmzfilename = query_name + ".kmz" print("\t\tNumber of Location Records: " + str(loc_records)) print("\t\t===> Saving KMZ to " + kmzfilename + "...") kml.savekmz(kmzfilename) except: print("\t\tERROR: Problem with database. Could be unsupported.")