Example #1
1
"""
This shows the use of the 3 different types of extended data: Data, Simple Data and Simple Array Data, as well as prettying up the data.
"""

import os
from simplekml import Kml, Types, Snippet, Color

# The KML
kml = Kml(name="ExtendedData", open=1)

# Data Example---------------------------------------------------------------------------------------------------------
# Create and style a point
pnt = kml.newpoint(name='1. World of Birds (Data)', coords =[(18.361960,-34.016543)])
pnt.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/paddle/1.png'

# Add the Data to the point
pnt.extendeddata.newdata(name='birds', value=400, displayname="Bird Species")
pnt.extendeddata.newdata(name='aviaries', value=100, displayname="Aviaries")
pnt.extendeddata.newdata(name='visitors', value=10000, displayname="Annual Visitors")


# Simple Data Example -------------------------------------------------------------------------------------------------
# Create a schema
schema = kml.newschema(name='WOW')

schema.newsimplefield(name='birds', type='int', displayname='Bird Species')
schema.newsimplefield(name='aviaries', type='int', displayname='Aviaries')
schema.newsimplefield(name='visitors', type='int', displayname='Annual Visitors')

# Create and style a point
pnt = kml.newpoint(name='2. World of Birds (Simple Data)', coords =[(18.361960,-34.017224)])
Example #2
0
 def get_kml(self, app_session):
     kml = Kml()
     stores = app_session.data_set.query(Store).all()
     for store in stores:
         kml.newpoint(name=store.name,
                      coords=[(store.longitude, store.latitude)])
     return kml.kml()
Example #3
0
def process():

    distances = sorted(compute_distances())

    # thin out the result by taking every 50th distance
    THIN = 50
    distances = distances[::THIN]
    pprint(distances)

    bearings = compute_bearings()

    locations = compute_locations(distances, bearings)

    points = compute_points(locations)

    kml = Kml()

    for distance, bearing, lat, lon in points:
        name = "d_%s_b_%s" % (distance, bearing)
        kml.newpoint(
            name=name,
            coords=[(lon, lat)]
        )

    kml.save("ayhe11.kml")
Example #4
0
def generate_kml():
    """Generate KML file from geojson."""
    uuid = request.values.get('uuid', None)
    campaign_name = request.values.get('campaign_name', None)
    campaign = Campaign(uuid)

    # Get json for each type.
    types = campaign.get_s3_types()
    if types is None:
        return Response(json.dumps({'error': 'types not found'}), 400)

    data = []
    for t in types:
        data.append(campaign.get_type_geojsons(t))

    if len(data) == 0:
        return Response(json.dumps({'error': 'Data not found'}), 400)

    features = [i['features'] for sublist in data for i in sublist]

    # for each type, we need to get geojson.
    kml = Kml(name=campaign_name)

    file_name = hashlib.md5(
        uuid.encode('utf-8') + '{:%m-%d-%Y}'.format(datetime.today()).encode(
            'utf-8')).hexdigest() + '.kml'

    file_path = os.path.join(config.CACHE_DIR, file_name)

    # For now, let's work only with points.
    # TODO: include polygons in the kml file.
    features = [[f for f in sublist if f['geometry']['type'] == 'Point']
                for sublist in features]
    features = [item for sublist in features for item in sublist]

    for feature in features:
        tags = feature['properties']['tags']
        extended_data = ExtendedData()
        kml_name = ''

        if 'name' in tags.keys():
            kml_name = tags['name']
        elif 'amenity' in tags.keys():
            kml_name = tags['amenity']

        [
            extended_data.newdata(k, escape(v)) for k, v in tags.items()
            if k != 'name'
        ]
        kml.newpoint(name=kml_name,
                     extendeddata=extended_data,
                     coords=[(feature['geometry']['coordinates'][0],
                              feature['geometry']['coordinates'][1])])
    kml.save(path=file_path)
    if kml:
        # Save file into client storage device.
        return Response(json.dumps({'file_name': file_name}))
Example #5
0
def google_earth_export():
    print('ttt'*100, request.form)
    form = GoogleEarthForm(request.form)
    if 'POST' in request.method:
        kml_file = Kml()
        for node in filter(lambda obj: obj.visible, Node.query.all()):
            point = kml_file.newpoint(name=node.name)
            point.coords = [(node.longitude, node.latitude)]
            point.style = styles[node.subtype]
            point.style.labelstyle.scale = request.form['label_size']

        for link in filter(lambda obj: obj.visible, Link.query.all()):
            line = kml_file.newlinestring(name=link.name)
            line.coords = [
                (link.source.longitude, link.source.latitude),
                (link.destination.longitude, link.destination.latitude)
            ]
            line.style = styles[link.type]
            line.style.linestyle.width = request.form['line_width']
        filepath = join(current_app.kmz_path, request.form['name'] + '.kmz')
        kml_file.save(filepath)
    return render_template(
        'google_earth_export.html',
        form=form
    )
Example #6
0
def make_kml(parsed_users, output_file, verbosity):
    """This function reads the user data supplied by ``parsed_users``, it then generates
    KML output and writes it to ``output_file``.

    Args:
        parsed_users (list): A list of lists, each sub_list should have 4 elements: ``[latitude, longitude, name, comment]``
        output_file (open): Location to save the KML output
        verbosity (int): If set to be >= ``1`` it will print out the string passed to ``message()``
    """
    kml = Kml()

    message("Making and writing KML to " + output_file, verbosity)
    for user in parsed_users:
        # Generate a KML point for the user.
        kml.newpoint(name=user[2], coords=[(user[1], user[0])], description=user[3])

    kml.save(output_file)
def job():
    """Compute the possible final locations for the geocache"""

    # open a new KML file
    kml = Kml()

    print(" r  c    dist   bear     lat        lon")

    # row checksum is 8
    for rows in range(8, 100, 9):

        # compute the distance in feet
        distance = rows * FEET_PER_ROW

        # caps checksum is 10
        for caps in range(10, 100, 9):

            # compute the bearing in degrees
            bearing = caps * DEGREES_PER_CAP

            # check for too far
            if bearing >= 360.:
                break

            # make the projection
            proj = projection(DOVE, distance, bearing)

            # get the components
            lat = proj.lat
            lon = proj.lon
            print("%2d %2d %8.2f %6.2f %f %f" %
                  (rows, caps, distance, bearing, lat, lon))

            # compute a name based on rows and caps
            name = "R%d_C%d" % (rows, caps)

            # add a new KML point
            kml.newpoint(name=name, coords=[(lon, lat)])

        print

    kml.save('cachemas2015_day10.kml')
Example #8
0
def main():
    """Compute longitude to fit with computed latitude and checksum."""
    kml = Kml()

    kml.newpoint(name="Vitts Mill", coords=[(HOME[1], HOME[0])])
    kml.newpoint(name="Vitts Mill WP2", coords=[(WP2[1], WP2[0])])

    # known values for A, B, C
    lat = "N38 27.%d%d%d" % (A, B, C)
    clat = convert(lat)

    # all answers sum to 24
    leftovers = 24 - (A + B + C)

    # compute all values for D, E and F
    for D in range(10):
        if D > leftovers:
            continue
        for E in range(10):
            if (D + E) > leftovers:
                continue
            for F in range(10):
                if D + E + F == leftovers:
                    lon = "W91 00.%d%d%d" % (D, E, F)
                    clon = convert(lon)
                    here = (clat, clon)
                    # compute distance from posted coordinates
                    d = vincenty(HOME, here).miles
                    print(d, lat, lon)
                    name = "loc_%d%d%d%d%d%d" % (A, B, C, D, E, F)
                    kml.newpoint(name=name, coords=[(clon, clat)])
    kml.save(FILENAME)
    print("Output is in %s" % FILENAME)
Example #9
0
def generate_kml():
    """Generate KML file from geojson."""
    features = request.values.get('location', None)
    uuid = request.values.get('uuid', None)
    campaign_name = request.values.get('campaign_name', None)
    if not features or not uuid:
        abort(404)
    features = json.loads(features)
    kml = Kml(name=campaign_name)

    file_name = hashlib.md5(
        uuid.encode('utf-8') + '{:%m-%d-%Y}'.format(datetime.today()).encode(
            'utf-8')).hexdigest() + '.kml'

    file_path = os.path.join(config.CACHE_DIR, file_name)

    for feature in features:
        if feature['type'] == 'Point':
            kml_name = ''
            extended_data = ExtendedData()

            if 'name' in feature['tags']:
                kml_name = feature['tags']['name']
            elif 'amenity' in feature['tags']:
                kml_name = feature['tags']['amenity']

            for key, value in feature['tags'].items():
                if key != 'name':
                    extended_data.newdata(key, value)

            kml.newpoint(name=kml_name,
                         extendeddata=extended_data,
                         coords=[(feature['latlon'][1], feature['latlon'][0])])

    kml.save(path=file_path)
    if kml:
        return Response(json.dumps({'file_name': file_name}))
Example #10
0
def view(view_type):
    view_options_form = ViewOptionsForm(request.form)
    google_earth_form = GoogleEarthForm(request.form)
    labels = {'node': 'name', 'link': 'name'}
    if 'view_options' in request.form:
        # retrieve labels
        labels = {
            'node': request.form['node_label'],
            'link': request.form['link_label']
        }
    elif 'google_earth' in request.form:
        kml_file = Kml()

        for node in filter(lambda obj: obj.visible, Node.query.all()):
            point = kml_file.newpoint(name=node.name)
            point.coords = [(node.longitude, node.latitude)]
            point.style = styles[node.subtype]
            point.style.labelstyle.scale = request.form['label_size']

        for link in filter(lambda obj: obj.visible, Link.query.all()):
            line = kml_file.newlinestring(name=link.name)
            line.coords = [(link.source.longitude, link.source.latitude),
                           (link.destination.longitude,
                            link.destination.latitude)]
            line.style = styles[link.type]
            line.style.linestyle.width = request.form['line_width']

        filepath = join(current_app.kmz_path, request.form['name'] + '.kmz')
        kml_file.save(filepath)

    return render_template(
        '{}_view.html'.format(view_type),
        view_options_form=view_options_form,
        google_earth_form=google_earth_form,
        labels=labels,
        names=pretty_names,
        subtypes=node_subtypes,
        node_table={
            obj:
            OrderedDict([(property, getattr(obj, property))
                         for property in type_to_public_properties[obj.type]])
            for obj in filter(lambda obj: obj.visible, Node.query.all())
        },
        link_table={
            obj:
            OrderedDict([(property, getattr(obj, property))
                         for property in type_to_public_properties[obj.type]])
            for obj in filter(lambda obj: obj.visible, Link.query.all())
        })
def process():
    kml = Kml()
    unique_locations = defaultdict(lambda: 0)
    add_static_waypoints(kml)
    for a in range(10):
        for b in range(10):
            for c in range(10):
                if c == 0:
                    continue
                q, r = divmod(a + b, c)
                if r:
                    continue


#               print a, b, c, q + 564, a * b * c + 87
                name = "Star_%d_%d_%d" % (a, b, c)
                lat = convert("N38 26.%03d" % (q + 564))
                lon = convert("W91 00.%03d" % (a * b * c + 87))
                coords = [(lon, lat)]
                print a, b, c, lat, lon
                kml.newpoint(name=name, coords=coords)
                unique_locations[coords[0]] += 1
    kml.save("star_of_the_east.kml")
    return unique_locations
Example #12
0
def writekml(inarray, kmlfilename):
    kml = Kml()
    sharedstyle = Style()
    sharedstyle.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/paddle/wht-blank.png'
    sharedstyle.iconstyle.color = "ffffffff"
    sharedstyle.iconstyle.ColorMode = "random"

    for indiv in numpy.nditer(inarray):
        desc = adddescription(indiv)
        lat =float(indiv[()]['Lat'])
        lon =float(indiv[()]['Long'])
        pnt = kml.newpoint(name=str(indiv[()]['GivenNames']) + " " +str(indiv[()]['FamilyName']), description=desc,
            coords = [(lon,lat)])
        pnt.style = sharedstyle
    kml.save(kmlfilename)
Example #13
0
def export_to_google_earth():
    kml_file = Kml()
    for device in fetch_all('Device'):
        point = kml_file.newpoint(name=device.name)
        point.coords = [(device.longitude, device.latitude)]
        point.style = styles[device.subtype]
        point.style.labelstyle.scale = request.form['label_size']
    for link in fetch_all('Link'):
        line = kml_file.newlinestring(name=link.name)
        line.coords = [(link.source.longitude, link.source.latitude),
                       (link.destination.longitude, link.destination.latitude)]
        line.style = styles[link.type]
        line.style.linestyle.width = request.form['line_width']
    filepath = app.path / 'google_earth' / f'{request.form["name"]}.kmz'
    kml_file.save(filepath)
    return True
Example #14
0
def export_to_google_earth():
    kml_file = Kml()
    for device in Device.query.all():
        point = kml_file.newpoint(name=device.name)
        point.coords = [(device.longitude, device.latitude)]
        point.style = styles[device.subtype]
        point.style.labelstyle.scale = request.form['label_size']
    for link in Link.query.all():
        line = kml_file.newlinestring(name=link.name)
        line.coords = [(link.source.longitude, link.source.latitude),
                       (link.destination.longitude, link.destination.latitude)]
        line.style = styles[link.type]
        line.style.linestyle.width = request.form['line_width']
    filepath = join(current_app.path, 'google_earth',
                    f'{request.form["name"]}.kmz')
    kml_file.save(filepath)
    return jsonify({'success': True})
Example #15
0
def export_to_google_earth():
    kml_file = Kml()
    for node in Node.query.all():
        point = kml_file.newpoint(name=node.name)
        point.coords = [(node.longitude, node.latitude)]
        point.style = styles[node.subtype]
        point.style.labelstyle.scale = request.form['label_size']

    for link in Link.query.all():
        line = kml_file.newlinestring(name=link.name)
        line.coords = [(link.source.longitude, link.source.latitude),
                       (link.destination.longitude, link.destination.latitude)]
        line.style = styles[link.type]
        line.style.linestyle.width = request.form['line_width']
    filepath = join(current_app.ge_path, request.form['name'] + '.kmz')
    kml_file.save(filepath)
    return jsonify({})
Example #16
0
    def from_networks(self, doc_name, networks):
        kml = Kml(name=doc_name)
        for n in networks:
            p = kml.newpoint(name=str(n))
            p.coords=[(n.lastlon,n.lastlat)]
            if(not n._lasttime):
                lasttime = "<i>None</i>"
            else:
                lasttime = n.formated_lasttime
                p.timestamp.when = n.lasttime.strftime("%Y-%m-%dT%H:%M:%SZ-05:00")
            
            if(not n.frequency):
                frequency = "<i>None</i>"
            else:
                frequency = "%s MHz" % n.frequency

            p.description = self._network_format % {"bssid": n.bssid, "ssid": (n.ssid or "<i>None</i>"), "capabilities": n.capabilities, "frequency": frequency, "lasttime": lasttime}

        return kml
Example #17
0
 def export_to_google_earth(self, **kwargs: Any) -> None:
     kml_file = Kml()
     for device in fetch_all("Device"):
         point = kml_file.newpoint(name=device.name)
         point.coords = [(device.longitude, device.latitude)]
         point.style = self.google_earth_styles[device.icon]
         point.style.labelstyle.scale = kwargs["label_size"]
     for link in fetch_all("Link"):
         line = kml_file.newlinestring(name=link.name)
         line.coords = [
             (link.source.longitude, link.source.latitude),
             (link.destination.longitude, link.destination.latitude),
         ]
         line.style = Style()
         kml_color = f"ff{link.color[5:]}{link.color[3:5]}{link.color[1:3]}"
         line.style.linestyle.color = kml_color
         line.style.linestyle.width = kwargs["line_width"]
     filepath = self.path / "projects" / "google_earth" / f'{kwargs["name"]}.kmz'
     kml_file.save(filepath)
Example #18
0
def kml(name, lon, lat, code=None, nc=None):
    from simplekml import Kml, Style
    from shapely import Polygon, Point
    if nc is not None:
        x = nc.variables['XLONG_M'][0, :, :]
        y = nc.variables['XLAT_M'][0, :, :]
        xc = nc.variables['XLONG_C'][0, :, :]
        yc = nc.variables['XLAT_C'][0, :, :]

    k = Kml()
    z = zip(name, lon, lat) if code is None else zip(name, lon, lat, code)
    for s in z:
        p = k.newpoint(name=s[3] if len(s) == 4 else s[0], coords=[s[1:3]])
        p.style.iconstyle.icon.href = "http://maps.google.com/mapfiles/kml/paddle/red-circle.png"
        p.style.balloonstyle.text = s[0]
        if nc is not None:
            i, j, d = nearest(x, y, s[1], s[2])
            coords = [(xc[i, j], yc[i, j]), (xc[i, j + 1], yc[i, j]),
                      (xc[i, j + 1], yc[i + 1, j]), (xc[i, j], yc[i + 1, j]),
                      (xc[i, j], yc[i, j])]
            if Polygon(coords).contains(Point(*s[1:3])):
                l = k.newlinestring(coords=[s[1:3], (x[i, j], y[i, j])])
                r = k.newlinestring(coords=coords)
    return k
Example #19
0
"""
This shows the use of the 3 different types of extended data: Data, Simple Data and Simple Array Data, as well as prettying up the data.
"""

import os
from simplekml import Kml, Types, Snippet, Color

# The KML
kml = Kml(name="ExtendedData", open=1)

# Data Example---------------------------------------------------------------------------------------------------------
# Create and style a point
pnt = kml.newpoint(name='1. World of Birds (Data)',
                   coords=[(18.361960, -34.016543)])
pnt.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/paddle/1.png'

# Add the Data to the point
pnt.extendeddata.newdata(name='birds', value=400, displayname="Bird Species")
pnt.extendeddata.newdata(name='aviaries', value=100, displayname="Aviaries")
pnt.extendeddata.newdata(name='visitors',
                         value=10000,
                         displayname="Annual Visitors")

# Simple Data Example -------------------------------------------------------------------------------------------------
# Create a schema
schema = kml.newschema(name='WOW')

schema.newsimplefield(name='birds', type='int', displayname='Bird Species')
schema.newsimplefield(name='aviaries', type='int', displayname='Aviaries')
schema.newsimplefield(name='visitors',
                      type='int',
Example #20
0
def createRoute(request):
    """
    Create Route is one of the main methods in the software because it
    creates the routes for each drone in the mission. It takes
    the POST['msg'] information. Then gets the square waypoints grid
    and divides it in many areas as drones we have. This method is
    triggered when the operator clicks on the play button.

    The areas are created and stored in the database and whe it's done
    we create a KML file/s and stores it in the KML_DIR path under the
    IBRI folder.

    @param request: Request petition that is handled by Django
    @type request: Django request object
    @return: Http response (JSON)
    @requires: utils.tsp
    @see: HttpResponse
    @see: AESCipher
    @see: JSON
    @see: csrf_extemp
    @see: WayPoint
    @see: Route
    @see: KML
    @see: utils.tsp.py
    @note: If you want to see how createRoute works, click on see source code.
    """

    if(request.method == 'POST'):

        # Get the preshared key and decrypt the post message.
        preshared = u'' + PRESHAREDKEY
        dec = json.loads(AESCipher(preshared).decrypt(request.POST['msg']))

        # Loads the json and stores the values in each variable
        base = json.loads(dec['base'])  # Base point
        coords = json.loads(dec['wayPoints'])  # Waypoints (lat, lng)
        nearPoint = json.loads(dec['nearPoint'])  # NearPoint
        userPK = json.loads(dec['insearch'])  # Users in search
        altitude = json.loads(dec['altitude'])  # Altitude

        # Number of areas
        nAreas = len(coords)
        totalElements = 0
        for i in coords:
            totalElements += len(i)

        size = int(sqrt(totalElements))
        nElements = int(math.floor(math.sqrt(len(coords[0]))))

        searchArea = [[[] for _ in range(nElements)] for _ in range(nAreas)]

        ov1 = 0
        ov2 = 0

        # Area divison
        for i in range(nAreas):

            for j in range(nElements):
                col = j * size

                if(ov1 == 0):
                    searchArea[i][j] = coords[i][(col-ov2):(col-ov2) + size]
                else:
                    searchArea[i][j] = coords[i][0:ov1]
                    ov2 = size - ov1
                    ov1 = 0

            if(len(searchArea[i][j]) == 0):
                searchArea[i].pop()
                j -= 1

            if(size - len(searchArea[i][j]) > 0):
                ov1 = size - len(searchArea[i][j])
                ov2 = 0
            else:
                ov1 = 0
                ov2 = 0

        coord = []
        x = 0

        # Assign coordenates to each area
        for i in range(nAreas):
            coord.append([])
            for j in searchArea[i]:
                for k in j:
                    coord[i].append([(x / size), (x % size)])
                    x += 1

        ac = 0
        route = []

        # Create an optimal route using the A* algorithm following the TSP
        for i in range(nAreas):

            n, D = mk_matrix(coord[i], distL2)
            tour = nearest_neighbor(n, (((nearPoint[i][0] * size)-ac) + nearPoint[i][1]), D)

            for j in range(len(tour)):

                tmpValue = tour[j] + (size - len(searchArea[i][0]))
                col = tmpValue / size

                if col == 0 and i > 0:
                    row = (tmpValue % size) - (size - len(searchArea[i][0]))
                    if DEBUG:
                        print(str(tour[j]) + " = " + str(col) + "," + str((tmpValue % size) - (size - len(searchArea[i][0]))))
                else:
                    row = tmpValue % size
                    if DEBUG:
                        print(str(tour[j])+" = "+str(col)+","+str(tmpValue % size))

                tour[j] = searchArea[i][col][row]

            ac += len(coord[i])
            route.append(tour)


        m = Mission()
        m.save()

        for userId in range(0, len(userPK)):
            m.inSearch.add(Clients.objects.get(pk=userPK[userId]))

        try:
            drones = Drone.objects.all()[0]
        except:
            print colored("Oops! No drones found un database", 'red')
            return HttpResponse('configerror')

        colorArray = [
            'ff00008b',  # darkred
            'ff8b0000',  # darkblue
            'ff006400',  # darkgreen
            'ff008cff',  # darkorange
            'ff9314ff',  # darkpink
            'ffff0000',  # blue
            'ff2fffad',  # greenyellow
            'ff5c5ccd',  # indianred
            'ffcbc0ff',  # pink
            'ffe16941',  # royalblue
            'ff00ffff',  # yellow
        ]

        drone_secuencial = 0

        # Creates the KML file
        from os import mkdir
        for r in route:

            rm = Route(mission=m, drone=Drone.objects.all()[drone_secuencial], baseLat=base[0], baseLng=base[1], initialWp=0)
            rm.save()
            tmpRoute = []
            tmpCounter = 0

            kmlName = 'IBRI' + str(m.id) + 'R' + str(rm.id)
            kml = Kml(name=kmlName)

            kml.newpoint(name="Base", coords=[(base[1], base[0])])

            pnt = kml.newlinestring(name='Route {}'.format(tmpCounter))
            coords = []

            for wp in r:
                coords.append((wp[1], wp[0], altitude))
                tmpRoute.append(WayPoint(route=rm, lat=wp[0], lng=wp[1], ref=tmpCounter))
                tmpCounter += 1

            pnt.coords = coords
            pnt.style.linestyle.width = 6
            pnt.style.linestyle.color = colorArray[drone_secuencial % len(colorArray)]

            pnt.altitudemode = AltitudeMode.relativetoground
            pnt.lookat.gxaltitudemode = GxAltitudeMode.relativetoseafloor
            pnt.lookat.latitude = coords[0][0]
            pnt.lookat.longitude = coords[0][1]
            pnt.lookat.range = 122
            pnt.lookat.heading = 0.063
            pnt.lookat.tilt = 0

            tmpRoute.append(WayPoint(route=rm, lat=base[0], lng=base[1], ref=tmpCounter))
            kml.newpoint(name="Back to base", coords=[(base[1], base[0])])

            rm.initialWp = len(tmpRoute)-2 # -2 for the last tmp counter and array 0 position
            rm.save()

            WayPoint.objects.bulk_create(tmpRoute)

            place = KML_DIR + '/IBRI' + str(m.id)

            try:
                mkdir(place)
            except Exception as e:
                print colored(e, 'red')
                pass

            place = place + '/' + kmlName + '.kml'
            place.replace('//', '/')

            try:
                kml.save(place)  # Saving
            except Exception as e:
                print colored(e, 'red')
                return HttpResponse('errorKmlPath')

            drone_secuencial += 1

        preshared = u'' + PRESHAREDKEY
        return HttpResponse(AESCipher(preshared).encrypt(json.dumps([m.pk, route])))
Example #21
0
# Check if path is valid directory or file
if not os.path.exists(args.path):
    print "Invalid path. Quitting."
    exit(1)

# Handle single JPG file or directory of JPG files
if args.directory:
    files = [os.path.join(args.path, f) for f in os.listdir(args.path) if re.match(r'[A-Za-z0-9-_]+.*\.(jpg|JPG)$', f)]
else:   
    files = [ args.path ]
    
### EXTRACT GPS DATA AND BUILD KML ###
kml = Kml(name=args.name)

cnt = 0
for fname in files:
    image = Image.open(fname)
    exif = extract_gpsinfo_from_image(image)
    latitude, longitude = get_lat_long(exif['GPSInfo'])
    
    print 'Adding %s at %s, %s...' % ( os.path.basename(fname), longitude, latitude )
    descr = '%s, %s' % ( longitude, latitude )
    kml.newpoint(name=os.path.basename(fname),
                description=descr,
                coords=[(longitude, latitude)])
    cnt = cnt + 1
    
kml.save(args.output)
#print kml.kml()
print '\nSuccessfully parsed %s files and generated %s' % ( cnt, args.output )
Example #22
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("pcap", help="path pcap file")
    parser.add_argument("-o", "--output", help="Output directory (must already exist)", default="./")
    parser.add_argument("-d", "--database", help="Database filename", default="sqlite.db")
    parser.add_argument("-k", "--kml", help="KML filename", default="results.kml")
    parser.add_argument("-r", "--report", help="Report filename", default="report.txt")
    args = parser.parse_args()

    # Check if pcap is a valid file
    if not os.path.exists(args.pcap):
        print "Invalid pcap file. Quitting."
        exit(1)
        
    # Check if output path is a valid file
    if not os.path.exists(args.output):
        print "Invalid output path. Quitting."
        exit(1)

    ### READ PCAP FILE ###
    pcap = rdpcap(args.pcap)
    files = extract_jpgs(pcap, args.output)
    
    ### INITIALIZE DATABASE ###
    conn = initdb(os.path.join(args.output,args.database))
    
    ### INITIALIZE KML ###
    kml = Kml(name=args.kml)
    
    for fname in files:
        
        ### EXTRACT EXIF DATA ###
        print "[+] Extracting exif data from " + fname
        image = Image.open(fname)
        exif = extract_exif(conn, image)
        
        ### GET MD5 ###
        md5hash = get_md5(os.path.basename(fname))
        print "[+] Getting md5 hash for " + os.path.basename(fname) + " => " + md5hash
        
        ### INSERT INTO DATABASE ###
        print "[+] Inserting record into database for " + fname
        insert_record(conn, os.path.basename(fname), md5hash, simplejson.dumps(exif))
        
        ### WRITE GPS INFO TO KML ###
        if exif["GPSInfo"]:
            latitude, longitude = get_lat_long(exif['GPSInfo'])
            print "[+] Writing GPS info (%s, %s) to KML for %s" % (longitude, latitude, os.path.basename(fname))
            descr = '%s, %s' % ( longitude, latitude )
            kml.newpoint(name=os.path.basename(fname),
                        description=descr,
                        coords=[(longitude, latitude)])
        
    ### SAVE KML TO FILE ###
    print "[+] Saving KML file to " + os.path.join(args.output, args.kml)
    kml.save(os.path.join(args.output, args.kml))
    
    ### GENERATE REPORT ###
    print "[+] Generating final report as " + os.path.join(args.output, args.report)
    with open(os.path.join(args.output, args.report), 'w') as r:
        now = strftime("%Y-%m-%d %H:%M:%S", gmtime())
        r.write("<==== Extraction Results ====>\n")
        r.write("Filename: %s\n" % args.pcap)
        r.write("Timestamp: %s GMT \n\n" % now)
        
        for row in conn.execute("SELECT * FROM results"):
            r.write("===================\n")
            r.write("Image: %s\n" % row[0])
            r.write("MD5 Hash: %s\n" % row[1])
            
            if row[2] and row[2] != "null":
                r.write("EXIF data:\n")
                json = simplejson.loads(row[2])
                for i in json:
                    r.write("\t%s: %s\n" % (i, json[i]))
                    if i == "GPSInfo":
                        latitude, longitude = get_lat_long(json['GPSInfo'])
                        r.write("\t%s (translated): %s, %s\n" % (i, latitude, longitude))
            else:
                r.write("No EXIF data found\n")
                        
            r.write("===================\n\n")
        
    conn.close()
Example #23
0
 def get_kml(self, app_session):
     kml = Kml()
     for tweet in app_session.data_set.query(Tweet).all():
         kml.newpoint(name=tweet.author + "-" + str(tweet.tweetId),
                      coords=[tweet.coordinates])
     return kml.kml()
Example #24
0
"""
How to use simplekml.
"""

import os
from simplekml import Kml

kml = Kml(
    name="Usage", open=1
)  # open=1 just opens the document in the TOC (table of contents). Not a necessary step.

# A simple Point
kml.newpoint(name="Kirstenbosch", coords=[(18.432314, -33.988862)])

# A simple Linestring showing off HTML markup
lin = kml.newlinestring(name="Pathway",
                        description="A pathway in <b>Kirstenbosch</b>",
                        coords=[(18.43312, -33.98924), (18.43224, -33.98914),
                                (18.43144, -33.98911), (18.43095, -33.98904)])

# A simple Polygon with a hole in it.
pol = kml.newpolygon(name="Atrium Garden",
                     outerboundaryis=[(18.43348, -33.98985),
                                      (18.43387, -33.99004262216968),
                                      (18.43410, -33.98972),
                                      (18.43371, -33.98952),
                                      (18.43348, -33.98985)],
                     innerboundaryis=[[(18.43360, -33.98982),
                                       (18.43386, -33.98995),
                                       (18.43401, -33.98974),
                                       (18.43376, -33.98962),
Example #25
0
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.")
Example #26
0
def view(view_type):
    add_node_form = AddNode(request.form)
    add_link_form = AddLink(request.form)
    view_options_form = ViewOptionsForm(request.form)
    google_earth_form = GoogleEarthForm(request.form)
    scheduling_form = SchedulingForm(request.form)
    scheduling_form.scripts.choices = Script.choices()
    scheduling_form.workflows.choices = Workflow.choices()
    labels = {'node': 'name', 'link': 'name'}
    if 'script' in request.form:
        data = dict(request.form)
        selection = map(int, session['selection'])
        scripts = request.form.getlist('scripts')
        workflows = request.form.getlist('workflows')
        data['scripts'] = [get_obj(Script, name=name) for name in scripts]
        data['workflows'] = [
            get_obj(Workflow, name=name) for name in workflows
        ]
        data['nodes'] = [get_obj(Node, id=id) for id in selection]
        data['user'] = current_user
        task = Task(**data)
        db.session.add(task)
        db.session.commit()
        return redirect(url_for('tasks_blueprint.task_management'))
    elif 'view_options' in request.form:
        # update labels
        labels = {
            'node': request.form['node_label'],
            'link': request.form['link_label']
        }
    elif 'google earth' in request.form:
        kml_file = Kml()
        for node in Node.query.all():
            point = kml_file.newpoint(name=node.name)
            point.coords = [(node.longitude, node.latitude)]
            point.style = styles[node.subtype]
            point.style.labelstyle.scale = request.form['label_size']

        for link in Link.query.all():
            line = kml_file.newlinestring(name=link.name)
            line.coords = [(link.source.longitude, link.source.latitude),
                           (link.destination.longitude,
                            link.destination.latitude)]
            line.style = styles[link.type]
            line.style.linestyle.width = request.form['line_width']
        filepath = join(current_app.ge_path, request.form['name'] + '.kmz')
        kml_file.save(filepath)
    # for the sake of better performances, the view defaults to markercluster
    # if there are more than 2000 nodes
    view = 'leaflet' if len(Node.query.all()) < 2000 else 'markercluster'
    if 'view' in request.form:
        view = request.form['view']
    # we clean the session's selected nodes
    session['selection'] = []
    # name to id
    name_to_id = {node.name: id for id, node in enumerate(Node.query.all())}
    return render_template(
        '{}_view.html'.format(view_type),
        filters=Filter.query.all(),
        view=view,
        scheduling_form=scheduling_form,
        view_options_form=view_options_form,
        google_earth_form=google_earth_form,
        add_node_form=add_node_form,
        add_link_form=add_link_form,
        labels=labels,
        names=pretty_names,
        subtypes=node_subtypes,
        name_to_id=name_to_id,
        node_table={
            obj:
            OrderedDict([(property, getattr(obj, property))
                         for property in type_to_public_properties[obj.type]])
            for obj in Node.query.all()
        },
        link_table={
            obj:
            OrderedDict([(property, getattr(obj, property))
                         for property in type_to_public_properties[obj.type]])
            for obj in Link.query.all()
        })
Example #27
0
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)
Example #28
0
'''
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')
Example #29
0
def csv_to_kml(input_filename):

	# open input file
	csv_file = open(input_filename,'rU')
	reader = csv.DictReader(csv_file)
	# preamble 
	input_filename_base, input_filename_ext = os.path.splitext(input_filename)
		
	# open output file
	kml_file = open(input_filename_base + '.kml','w')

	kml_file.write(r"""<?xml version="1.0" encoding="utf-8" ?>
	<kml xmlns="http://www.opengis.net/kml/2.2">
	""")
	kml_file.write("<Document><name>%s</name>" % input_filename_base)
	kml_file.write(r""" <Style id="grid1k"><IconStyle> <Icon> <color>ff0000</color> </Icon> </IconStyle></Style>""")
	
	kml_file.write(r"""
	<Schema name="sample" id="sample">
		<SimpleField name="Name" type="string"></SimpleField>
		<SimpleField name="Description" type="string"></SimpleField>
		<SimpleField name="GID" type="string"></SimpleField>
	</Schema>
	""")
	gids_unique = set()
	gids = []
	locs_1k = []        
	# main loop 
	for line in reader:

		kml_file.write('  <Placemark>\n')
		kml_file.write('  <name>GID=%s</name>\n' % (line['GID_100m']))
		kml_file.write('\t<ExtendedData><SchemaData schemaUrl=\"#sample\">\n')
		kml_file.write('  <SimpleField name="GID">%s</SimpleField>\n' % (line['GID_100m']))
		kml_file.write('\t\t</SchemaData></ExtendedData>\n')
		kml_file.write("     <Point><coordinates>%s,%s</coordinates></Point>\n" % (line['x'], line['y']))
		kml_file.write('  </Placemark>\n')
	        
	        gids_unique.add(line['GID_1k'])
	        gids.append(line['GID_1k'])
                locs_1k.append([line['x_1k'], line['y_1k']])

	# epilogue
	kml_file.write('\t</Document>\n\t</kml>')
	csv_file.close()
	kml_file.close()
	gids_unique = list(gids_unique)
	locs_1k_unique = []
	for gid in gids_unique:
	    locs_1k_unique.append([locs_1k[k] for k, x in enumerate(map(lambda x: x==gid, gids)) if x][0])
	
	for i, loc in enumerate(locs_1k_unique):
	    kml=Kml()
	    proj_para =  "+proj=laea +ellps=WGS84 +lon_0=20 +lat_0=5 +units=m +no_defs"
	    project = pyproj.Proj(proj_para)
	    loc_laea = list(project(loc[0], loc[1]))
	    center_pt = kml.newpoint(name=gids_unique[i], description="1k by 1k grid", coords=[(loc[0], loc[1])])
            pol = kml.newpolygon(name="1k grid", description="A pathway in Kirstenbosch",  
                                 outerboundaryis=[project(loc_laea[0]-500, loc_laea[1]+500, inverse=True), 
                                                  project(loc_laea[0]+500, loc_laea[1]+500, inverse=True), 
                                                  project(loc_laea[0]+500, loc_laea[1]-500, inverse=True),
                                                  project(loc_laea[0]-500, loc_laea[1]-500, inverse=True), 
                                                  project(loc_laea[0]-500, loc_laea[1]+500, inverse=True)],
                                 innerboundaryis=[project(loc_laea[0]-500, loc_laea[1]+500, inverse=True), 
                                                  project(loc_laea[0]+500, loc_laea[1]+500, inverse=True), 
                                                  project(loc_laea[0]+500, loc_laea[1]-500, inverse=True),
                                                  project(loc_laea[0]-500, loc_laea[1]-500, inverse=True), 
                                                  project(loc_laea[0]-500, loc_laea[1]+500, inverse=True)])
            pol.polystyle.color = 'ff0000ff' 
	    kml.save("csv/"+gids_unique[i]+".kml")
Example #30
0
import gspread
from simplekml import Kml

gc = gspread.service_account(filename="credentials.json")
sh = gc.open_by_key('12zEDmifmfdcECMwoQdldpZAe7Asshow7cUzRyWF-uRI')
worksheet = sh.sheet1
res = worksheet.get_all_values()

kml = Kml(name='resources')
for n in res[1:]:  #skip the first index and ignore the Nos
    if (n[1] != 'No'):
        kml.newpoint(name=n[2], coords=[(n[3], n[4])])

kml.save("KmlClass.kml")  # Saving
Example #31
0
def csv_to_kml(input_filename):

	# open input file
	csv_file = open(input_filename,'rU')
	reader = csv.DictReader(csv_file)
	# preamble 
	input_filename_base, input_filename_ext = os.path.splitext(input_filename)
	print(input_filename_base)	
	# open output file
	kml_file = open(input_filename_base + '.kml','w')

	kml_file.write(r"""<?xml version="1.0" encoding="utf-8" ?>
	<kml xmlns="http://www.opengis.net/kml/2.2">
	""")
	kml_file.write("<Document><name>%s</name>" % input_filename_base)
	kml_file.write(r""" <Style id="grid1k"><IconStyle> <Icon> <color>ff0000</color> </Icon> </IconStyle></Style>""")
	
	kml_file.write(r"""
	<Schema name="sample" id="sample">
		<SimpleField name="Name" type="string"></SimpleField>
		<SimpleField name="Description" type="string"></SimpleField>
		<SimpleField name="GID" type="string"></SimpleField>
	</Schema>
	""")
	gids_unique = set()
	gids = []
	locs_1k = []        
	# main loop 
	for line in reader:

		kml_file.write('  <Placemark>\n')
		kml_file.write('  <name>GID=%s</name>\n' % (line['GID_100m']))
		kml_file.write('\t<ExtendedData><SchemaData schemaUrl=\"#sample\">\n')
		kml_file.write('  <SimpleField name="GID">%s</SimpleField>\n' % (line['GID_100m']))
		kml_file.write('\t\t</SchemaData></ExtendedData>\n')
		kml_file.write("     <Point><coordinates>%s,%s</coordinates></Point>\n" % (line['x'], line['y']))
		kml_file.write('  </Placemark>\n')
	        
	        gids_unique.add(line['GID_1k'])
	        gids.append(line['GID_1k'])
                locs_1k.append([line['x_1k'], line['y_1k']])

	# epilogue
	kml_file.write('\t</Document>\n\t</kml>')
	csv_file.close()
	kml_file.close()

	gids_unique = list(gids_unique)
	locs_1k_unique = []
	for gid in gids_unique:
	    locs_1k_unique.append([locs_1k[k] for k, x in enumerate(map(lambda x: x==gid, gids)) if x][0])
	
	for i, loc in enumerate(locs_1k_unique):
	    kml=Kml()
	    proj_para =  "+proj=laea +ellps=WGS84 +lon_0=20 +lat_0=5 +units=m +no_defs"
	    project = pyproj.Proj(proj_para)
	    loc_laea = list(project(loc[0], loc[1]))
	    center_pt = kml.newpoint(name=gids_unique[i], description="1k by 1k grid", coords=[(loc[0], loc[1])])
            pol = kml.newpolygon(name="1k grid", description="A pathway in Kirstenbosch",  
                                 outerboundaryis=[project(loc_laea[0]-500, loc_laea[1]+500, inverse=True), 
                                                  project(loc_laea[0]+500, loc_laea[1]+500, inverse=True), 
                                                  project(loc_laea[0]+500, loc_laea[1]-500, inverse=True),
                                                  project(loc_laea[0]-500, loc_laea[1]-500, inverse=True), 
                                                  project(loc_laea[0]-500, loc_laea[1]+500, inverse=True)],
                                 innerboundaryis=[project(loc_laea[0]-500, loc_laea[1]+500, inverse=True), 
                                                  project(loc_laea[0]+500, loc_laea[1]+500, inverse=True), 
                                                  project(loc_laea[0]+500, loc_laea[1]-500, inverse=True),
                                                  project(loc_laea[0]-500, loc_laea[1]-500, inverse=True), 
                                                  project(loc_laea[0]-500, loc_laea[1]+500, inverse=True)])
            pol.polystyle.color = 'ff0000ff' 
	    kml.save("output/drone_flight_1k/"+gids_unique[i]+".kml")
Example #32
0
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***WARNING***: 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 or database schemas (ie: cache_encryptedB.db or knowledgeC.db). If it should work, try using chown/chmod to change permissions/ownership."
            )
Example #33
0
Styling points, linestrings, polygons and TOC items.
"""

import os
from simplekml import Kml, ListItemType, Color

kml = Kml(name="Styling", open=1)

# Make all the items into radio buttons
kml.document.liststyle.listitemtype = ListItemType.radiofolder

# Change the icon of the document in the TOC
kml.document.liststyle.itemicon.href = "http://maps.google.com/mapfiles/kml/shapes/parks.png"

# A normal Point with both a LabelStyle and IconStyle
pnt = kml.newpoint(name="Kirstenbosch Normal", description="A style map.", coords=[(18.431486,-33.988)])
pnt.labelstyle.color = 'ff0000ff'
pnt.labelstyle.scale = 2  # Text twice as big
pnt.labelstyle.color = "ffff0000"
pnt.iconstyle.color = 'ffff0000'  # Blue
pnt.iconstyle.scale = 3  # Icon thrice as big
pnt.iconstyle.icon.href = 'http://maps.google.com/mapfiles/kml/shapes/info-i.png'  # Culry 'information i

# A Point with a styleMap. The Text changes from blue to red on mouse over.
pnt = kml.newpoint(name="Kirstenbosch StyleMap", coords=[(18.432314,-33.988862)])
pnt.stylemap.normalstyle.labelstyle.color = 'ffff0000'
pnt.stylemap.highlightstyle.labelstyle.color = 'ff0000ff'

# A red thick LineString
lin = kml.newlinestring(name="Pathway", description="A pathway in Kirstenbosch",
                        coords=[(18.43312,-33.98924), (18.43224,-33.98914), (18.43144,-33.98911), (18.43095,-33.98904)])
Example #34
0
        
        if firstPos.newTime.hour < 12 or (firstPos.newTime.hour == 12 and firstPos.newTime.minute < 30):
            #blue line
            line.style.linestyle.color = 'ffff0000'
            
        else:
            #red line
            line.style.linestyle.color = 'ff0000ff'
    
    if nmeaLog.dumpLog != None :
        for dumpFile in nmeaLog.dumpLog.dumps:
            if dumpFile.latitude == None or dumpFile.longitude == None or (dumpFile.latitude == 0.0 and dumpFile.longitude == 0.0):
                continue
        
            dumpTime = str(dumpFile.dumpEvent.newTime.hour)+":"+str(dumpFile.dumpEvent.newTime.minute)+":"+str(dumpFile.dumpEvent.newTime.second)
            point = kml.newpoint(name="dump_"+str(dumpFile.UID)+"_"+dumpTime, description="",coords=[(dumpFile.longitude,dumpFile.latitude)])
            
            if dumpFile.UID in UIDtoColor:
                 point.style.iconstyle.icon.href = UIDtoColor[dumpFile.UID]
            else:
                print "    warning unknown UID : "+str(dumpFile.UID)

    else:
        print "    warning, no dumpLog linked to "+str(nmeaLog)


kml.save(kmlDirectory+"skidump_"+str(currentNmeaLogDate.day)+"_"+str(currentNmeaLogDate.month)+"_"+str(currentNmeaLogDate.year)+".kml")

###### build a file with all the cable car #######################################################################
kml = Kml()
for line in lineList:
Example #35
0
def flask_get_kml_feed():
    """ Return KML with RS telemetry """
    kml = Kml()
    kml.resetidcounter()
    kml.document.name = "Track"
    kml.document.open = 1
    # Station Placemark
    pnt = kml.newpoint(
        name="Ground Station",
        altitudemode=AltitudeMode.absolute,
        description="AutoRX Ground Station",
    )
    pnt.open = 1
    pnt.iconstyle.icon.href = flask.request.host_url + "static/img/antenna-green.png"
    pnt.coords = [
        (
            autorx.config.global_config["station_lon"],
            autorx.config.global_config["station_lat"],
            autorx.config.global_config["station_alt"],
        )
    ]
    for rs_id in flask_telemetry_store:
        try:
            coordinates = []

            for tp in flask_telemetry_store[rs_id]["track"].track_history:
                coordinates.append((tp[2], tp[1], tp[3]))

            rs_data = """\
            {type}/{subtype}
            Frequency: {freq}
            Altitude: {alt:.1f} m
            Heading: {heading:.1f} degrees
            Ground Speed: {vel_h:.2f} m/s
            Ascent Rate: {vel_v:.2} m/s
            Temperature: {temp:.1f} C
            Humidity: {humidity:.1f} %
            Pressure: {pressure:.1f} hPa
            """
            if flask_telemetry_store[rs_id]["latest_telem"]["vel_v"] > -5:
                icon = flask.request.host_url + "static/img/balloon-green.png"
            else:
                icon = flask.request.host_url + "static/img/parachute-green.png"

            # Add folder
            fol = kml.newfolder(name=rs_id)
            # HAB Placemark
            pnt = fol.newpoint(
                name=rs_id,
                altitudemode=AltitudeMode.absolute,
                description=rs_data.format(
                    **flask_telemetry_store[rs_id]["latest_telem"]
                ),
            )
            pnt.iconstyle.icon.href = icon
            pnt.coords = [
                (
                    flask_telemetry_store[rs_id]["latest_telem"]["lon"],
                    flask_telemetry_store[rs_id]["latest_telem"]["lat"],
                    flask_telemetry_store[rs_id]["latest_telem"]["alt"],
                )
            ]
            linestring = fol.newlinestring(name="Track")
            linestring.coords = coordinates
            linestring.altitudemode = AltitudeMode.absolute
            linestring.extrude = 1
            linestring.stylemap.normalstyle.linestyle.color = "ff03bafc"
            linestring.stylemap.highlightstyle.linestyle.color = "ff03bafc"
            linestring.stylemap.normalstyle.polystyle.color = "AA03bafc"
            linestring.stylemap.highlightstyle.polystyle.color = "CC03bafc"
            # Add LOS line
            linestring = fol.newlinestring(name="LOS")
            linestring.altitudemode = AltitudeMode.absolute
            linestring.coords = [
                (
                    autorx.config.global_config["station_lon"],
                    autorx.config.global_config["station_lat"],
                    autorx.config.global_config["station_alt"],
                ),
                (
                    flask_telemetry_store[rs_id]["latest_telem"]["lon"],
                    flask_telemetry_store[rs_id]["latest_telem"]["lat"],
                    flask_telemetry_store[rs_id]["latest_telem"]["alt"],
                ),
            ]
        except Exception as e:
            logging.error(
                "KML - Could not parse data from RS %s - %s" % (rs_id, str(e))
            )

    return (
        re.sub("<Document.*>", "<Document>", kml.kml()),
        200,
        {"content-type": "application/vnd.google-earth.kml+xml"},
    )
class kmlManager(object):
    def init(self):
        self.kml_list_start_time               = None
        self.kml_list                          = []
        self.kml_interest_line_list            = []
        self.kml_interest_point_list           = []
        
    def __init__(self, line_limit_point, pathDirectory):
        self.kml = Kml()
        self.init()
        self.kml_interest_line_already_meeted  = {}
        self.kml_interest_point_already_meeted = {}
        self.line_limit_point                  = line_limit_point
        self.pathDirectory = pathDirectory
        self.midday = (12,0,)
    
    def addPointOfInterest(self,point):
        if point.descr not in self.kml_interest_point_already_meeted: #not yet printed ?
            self.kml_interest_point_already_meeted[point.descr] = True
            self.kml_interest_point_list.append(point)

    def addLineOfInterest(self, line):
        if line.descr not in self.kml_interest_line_already_meeted: #not yet printed ?
            self.kml_interest_line_already_meeted[line.descr] = True
            self.kml_interest_line_list.append(line)
            
    def addLinePoint(self,gpoint, utcdatetime, colorMarker):
        if self.line_limit_point > 0:#don't want to write the line of the road ?
            if len(self.kml_list) == 0: #set start time
                self.kml_list_start_time = utcdatetime
                
            self.kml_list.append( (gpoint.lat, gpoint.lon,) )
        
        if len(self.kml_list) >= self.line_limit_point:
            #kml process
            if len(self.kml_list) > 0:
                line = self.kml.newlinestring(name="From "+self.kml_list_start_time.isoformat()+" to "+utcdatetime.isoformat(), description="", coords=self.kml_list)
            
                #if past midday, colour the line in red
                if utcdatetime.hour < self.midday[0] or (utcdatetime.hour == self.midday[0] and utcdatetime.minute < self.midday[1]):
                    line.style.linestyle.color = 'afff0000'#morning, blue line
                else:
                    line.style.linestyle.color = 'af0000ff'#afternoon, red line
            
            #write the meeted line of interest
            for interest_line in self.kml_interest_line_list:
                #line
                line = self.kml.newlinestring(name=interest_line.descr, description="", coords=((interest_line.start.lat,interest_line.start.lon,), (interest_line.end.lat,interest_line.end.lon,),))
                line.style.linestyle.color = 'af00ff00' #green
                
                #start point
                point = self.kml.newpoint(name="Start point of "+interest_line[2], description="",coords=(interest_line.start.lat,interest_line.start.lon,))
                point.style.iconstyle.icon.href = "http://maps.google.com/mapfiles/dd-start.png"
                
                #end point
                point = self.kml.newpoint(name="End point of "+interest_line[2], description="",coords=(interest_line.end.lat,interest_line.end.lon,))
                point.style.iconstyle.icon.href = "http://maps.google.com/mapfiles/dd-end.png"
                
            #write the meeted point of interest
            for interest_point in self.kml_interest_point_list:
                point = self.kml.newpoint(name=interest_point.name, description=interest_point.descr,coords=( (interest_point.lat, interest_point.lon,), ))
                point.style.iconstyle.icon.href = colorMarker.getColorPath(interest_point.name)
           
            #save the file (for every line written, overwrite the file)
            date = datetime.datetime.now()
            self.kml.save(self.pathDirectory+"skidump_"+str(date.day)+"_"+str(date.month)+"_"+str(date.year)+".kml")
            
            #reset list
            self.init()

    def addEventPointList(self,l):
        #get point of interest from proxy
        self.kml_interest_point_list.extend(l)
from simplekml import Kml, Color
kml = Kml(open=1)

# generate geometries
point = kml.newpoint(name="TestPoint", coords=[(-23.094721, 33.4838)])

linestring = kml.newlinestring(name="TestLinestring")
linestring.coords = [(-23.1659603, 33.4754108), (-23.1631279, 33.4767710),
                     (-23.1604671, 33.4769858), (-23.1554890, 33.4758403),
                     (-23.1545448, 33.4731198), (-23.1518841, 33.4686807),
                     (-23.1486225, 33.4667476), (-23.1433010, 33.4662464),
                     (-23.1391811, 33.4666044), (-23.1354904, 33.4686807),
                     (-23.1324005, 33.4715447), (-23.1305981, 33.4754824),
                     (-23.1307697, 33.4795631), (-23.1289673, 33.4822835),
                     (-23.1247616, 33.4828562), (-23.1208992, 33.4819255),
                     (-23.1177235, 33.4796347), (-23.1159210, 33.4762699),
                     (-23.1117153, 33.4736925), (-23.1081963, 33.4726186)]

polygon1 = kml.newpolygon(name="TestPolygonOnlyOuter")
polygon1.outerboundaryis = [
    (-23.0895710, 33.4706855), (-23.0868244, 33.4729050),
    (-23.0818462, 33.4744085), (-23.0760098, 33.4738357),
    (-23.0727482, 33.4737641), (-23.0702591, 33.4745517),
    (-23.0689716, 33.4755540), (-23.0683708, 33.4769142),
    (-23.0689716, 33.4778449), (-23.0709457, 33.4772722),
    (-23.0737782, 33.4761267), (-23.0778122, 33.4772006),
    (-23.0790138, 33.4786324), (-23.0786705, 33.4804938),
    (-23.0774689, 33.4819255), (-23.0756664, 33.4823551),
    (-23.0742931, 33.4837868), (-23.0740356, 33.4856480),
    (-23.0724049, 33.4870797), (-23.0697441, 33.4884398),
    (-23.0687141, 33.4880103), (-23.0688000, 33.4871513),
Example #38
0
    exit(1)

# Handle single JPG file or directory of JPG files
if args.directory:
    files = [
        os.path.join(args.path, f) for f in os.listdir(args.path)
        if re.match(r'[A-Za-z0-9-_]+.*\.(jpg|JPG)$', f)
    ]
else:
    files = [args.path]

### EXTRACT GPS DATA AND BUILD KML ###
kml = Kml(name=args.name)

cnt = 0
for fname in files:
    image = Image.open(fname)
    exif = extract_gpsinfo_from_image(image)
    latitude, longitude = get_lat_long(exif['GPSInfo'])

    print 'Adding %s at %s, %s...' % (os.path.basename(fname), longitude,
                                      latitude)
    descr = '%s, %s' % (longitude, latitude)
    kml.newpoint(name=os.path.basename(fname),
                 description=descr,
                 coords=[(longitude, latitude)])
    cnt = cnt + 1

kml.save(args.output)
#print kml.kml()
print '\nSuccessfully parsed %s files and generated %s' % (cnt, args.output)