예제 #1
0
def map(orbit_data, output_file, colour, width, indices):
    """Maps GMAT data from the file orbit_data as a KMZ file"""
    print "Creating map for orbit file", orbit_data
    coords = []
    if not os.path.exists(orbit_data):
        raise IOError('File "%s" does not exist' % orbit_data)
    with open(orbit_data, 'r') as orbit:
        for i, row in enumerate(orbit):
            if indices[0] <= i <= indices[1] and i % 2 == 0:
                try:
                    lat, lon = [float(x) for x in row.split()[:2]]
                except:
                    print row
                    print i
                    raise
                coords.append((lon, lat))
            else:
                pass

    line_style = KML.LineStyle(colour=colour, width=width, id='GroundTrack')

    print "Making KML"
    coord = KML.Coord(coords)
    track = KML.LineString(
        coord,
        style=line_style,
        name='GMAT Orbit Track',
        description='Ground track for GMAT orbit from file %s' %
        os.path.split(orbit_data)[-1])

    map = KML.KML(name='GMAT Orbit')
    map.add_object(track)

    if output_file is None:
        output_file = os.path.join(os.path.splitext(orbit_data)[0], '.kmz')
    map.write(output_file)
    print "File saved as", os.path.realpath(output_file)