Python


KML

def draw_kml(df, lon_label, lat_label, fname):

    kml = simplekml.Kml()

    STYLE_I = simplekml.Style()
    STYLE_I.iconstyle.icon.href = 'http://maps.google.co.jp/mapfiles/ms/icons/red-dot.png'
    STYLE_I.labelstyle.scale = 0.66
    STYLE_I.labelstyle.color = 'aaffffff'

    STYLE_LS = {4 : simplekml.Style(),
                5 : simplekml.Style(),
                1 : simplekml.Style()}
    for key, style in STYLE_LS.items():
        style.linestyle.width = 3

    STYLE_LS[4].linestyle.color = simplekml.Color.blue
    STYLE_LS[5].linestyle.color = simplekml.Color.red
    STYLE_LS[1].linestyle.color = 'ff505050'

    fol_time = kml.newfolder(name='時刻')
    fol_line = kml.newfolder(name='軌跡')
    fol_line_type = {}
    fol_line_type[4] = fol_line.newfolder(name='4')
    fol_line_type[5] = fol_line.newfolder(name='5')
    fol_line_type[1] = fol_line.newfolder(name='1')

    df_pnt = df.asfreq('S')
    for i in range(len(df_pnt)):
        sr = df_pnt.iloc[i,:]
        desc = sr.name.strftime('%H:%M:%S.%f')[:-3]
        pnt = fol_time.newpoint(name=desc, coords=[sr[[lon_label, lat_label]]])
        pnt.style = STYLE_I
        pnt.visibility = 0

    df_line = copy.deepcopy(df)
    sr_q = df['quality']
    df_line['q_tmp'] = (sr_q != sr_q.shift()).cumsum() - 1
    display(df_line)
    for key, val in df_line.groupby('q_tmp').groups.items():
        df_tmp = df_line.groupby('q_tmp').get_group(key)
        q = df_tmp.iloc[0,:]['quality']
        start_time = df_tmp.iloc[0,:].name.strftime('%H:%M:%S.%f')[:-3]
        end_time = df_tmp.iloc[-1,:].name.strftime('%H:%M:%S.%f')[:-3]
        df_end = df_line[df_tmp.iloc[-1,:].name:]
        coords = list(zip(df_tmp[lon_label], df_tmp[lat_label]))
        end_coord = []
        if 1 < len(df_end):
            end_coord = [(df_end.iloc[1,:][lon_label], df_end.iloc[1,:][lat_label])]
        ls = fol_line_type[q].newlinestring(name=start_time + ' - ' + end_time,
                                            coords=coords+end_coord)
        ls.style = STYLE_LS[q]
       
    kml.save(fname)

        
lonlat_dict = {}
lonlat_dict['2017/03/22 15:16:44.8'] = [135.069, 34.389, 4]
lonlat_dict['2017/03/22 15:16:45.0'] = [135.070, 34.389, 4]
lonlat_dict['2017/03/22 15:16:45.2'] = [135.070, 34.390, 4]
lonlat_dict['2017/03/22 15:16:45.4'] = [135.071, 34.391, 4]
lonlat_dict['2017/03/22 15:16:45.6'] = [135.073, 34.392, 5]
lonlat_dict['2017/03/22 15:16:45.8'] = [135.073, 34.393, 5]
lonlat_dict['2017/03/22 15:16:46.0'] = [135.074, 34.394, 5]
lonlat_dict['2017/03/22 15:16:46.2'] = [135.075, 34.394, 5]
lonlat_dict['2017/03/22 15:16:46.4'] = [135.076, 34.394, 5]
lonlat_dict['2017/03/22 15:16:46.6'] = [135.077, 34.394, 4]
lonlat_dict['2017/03/22 15:16:46.8'] = [135.078, 34.395, 4]
lonlat_dict['2017/03/22 15:16:47.0'] = [135.079, 34.395, 4]
lonlat_dict['2017/03/22 15:16:47.2'] = [135.080, 34.395, 5]
lonlat_dict['2017/03/22 15:16:47.4'] = [135.081, 34.395, 4]
lonlat_dict['2017/03/22 15:16:47.6'] = [135.082, 34.396, 4]
lonlat_dict['2017/03/22 15:16:47.8'] = [135.082, 34.397, 1]
lonlat_dict['2017/03/22 15:16:48.0'] = [135.083, 34.398, 1]
lonlat_dict['2017/03/22 15:16:48.2'] = [135.083, 34.399, 1]

df = pd.DataFrame.from_dict(lonlat_dict, orient='index', columns=['lon', 'lat', 'quality'])
df.index = pd.to_datetime(df.index)
display(df)

draw_kml(df, 'lon', 'lat', './out.kml')

最新の20件

2021-03-31 2020-06-09
  • Python
2020-05-30 2019-12-02 2018-10-18 2018-07-05 2018-01-11 2015-12-07 2015-08-03 2015-07-20 2012-07-19 2012-06-30 2012-06-23 2012-01-25 2011-11-09 2011-11-08 2011-10-02

今日の15件

  • counter: 198
  • today: 1
  • yesterday: 0
  • online: 1