def read_ais_data(self): for file, color in zip(self.data_paths, self.colors): one_MMSI = [] st_MMSI = [] parse.get_data_ST(file, st_MMSI) shipdata = st_MMSI[0] shipMMSI = shipdata['MMSI'] Length = int(shipdata['StaticInfo'][0]['Length']) Width = int(shipdata['StaticInfo'][0]['Width']) Draught = int(shipdata['StaticInfo'][0]['Draught']) Dun = Length * Width * Draught parse.get_data_DY(file, one_MMSI) x_list = [] y_list = [] date_list = [] Dun_list = [] for shipdata in one_MMSI: shipMMSI = shipdata['MMSI'] x = shipdata['DynamicInfo'][0]['Longitude'] # jingdu y = shipdata['DynamicInfo'][0]['Latitude'] # weidu datetime = shipdata['DynamicInfo'][0]['LastTime'] date_list.append(datetime) x_list.append(x) y_list.append(y) Dun_list.append(Dun) date = np.array(date_list) x = np.array(x_list) for i in range((x.shape)[0]): if x[i] <= 0: x[i] = 360 + x[i] # correct longtitude y = np.array(y_list) table = pd.DataFrame(columns=['Date', 'X', 'Y', 'Dun']) table['Date'] = date table['X'] = x table['Y'] = y table['Dun'] = np.array(Dun_list) table.set_index('Date', drop=False, append=False, inplace=True, verify_integrity=False) table = table.sort_index() self.tables.append(table)
def Classification_By_Speed(PathName, Output): data_paths = [os.path.join(PathName, s) for s in os.listdir(PathName)] speed_distribution = {} for file in data_paths: one_MMSI = [] parse.get_data_DY(file, one_MMSI) sp = one_MMSI[0]['DynamicInfo'][0]['Speed'] if sp <= 0: continue if speed_distribution.has_key(int(sp)): speed_distribution[int(sp)] += 1 else: speed_distribution[int(sp)] = 1 print speed_distribution with open(Output, 'w') as f: json.dump(speed_distribution, f)
def map_AllLine(DirectoryPath): def compute_date(date0, date1): d1 = pdatetime.strptime(date0, '%Y-%m-%d %H:%M:%S') d2 = pdatetime.strptime(date1, '%Y-%m-%d %H:%M:%S') return (d2 - d1).days map = Basemap(projection='mill', lon_0=180, width=12000000, height=9000000) map.drawcoastlines() map.drawparallels(np.arange(-90, 90, 30), labels=[1, 0, 0, 0]) map.drawmeridians(np.arange(map.lonmin, map.lonmax + 30, 60), labels=[0, 0, 0, 1]) map.drawmapboundary(fill_color='white') map.drawcoastlines(linewidth=0.5, color='y') map.drawcountries(color='y') map.drawstates(color='y') map.fillcontinents(color='coral', lake_color='blue') colors = cycle(allcolors.values()) data_paths = [os.path.join(DirectoryPath, s) for s in os.listdir(DirectoryPath)] counter = 1 for file,color in zip(data_paths,colors): one_MMSI = [] st_MMSI = [] parse.get_data_ST(file,st_MMSI) shipdata = st_MMSI[0] shipMMSI = shipdata['MMSI'] Length = int(shipdata['StaticInfo'][0]['Length']) Width = int(shipdata['StaticInfo'][0]['Width']) Draught = int(shipdata['StaticInfo'][0]['Draught']) Dun = Length * Width * Draught parse.get_data_DY(file, one_MMSI) x_list = [] y_list = [] date_list = [] Dun_list = [] for shipdata in one_MMSI: shipMMSI = shipdata['MMSI'] x = shipdata['DynamicInfo'][0]['Longitude'] # jingdu y = shipdata['DynamicInfo'][0]['Latitude'] # weidu datetime = shipdata['DynamicInfo'][0]['LastTime'] date_list.append(datetime) x_list.append(x) y_list.append(y) Dun_list.append(Dun) date = np.array(date_list) x = np.array(x_list) for i in range((x.shape)[0]): if x[i] <= 0: x[i] = 360 + x[i] # correct longtitude y = np.array(y_list) table = pd.DataFrame(columns=['Date', 'X', 'Y' , 'Dun']) table['Date'] = date table['X'] = x table['Y'] = y table['Dun'] = np.array(Dun_list) table.set_index('Date', drop=False, append=False, inplace=True, verify_integrity=False) table = table.sort_index() # table.to_csv('look.csv') # table = table.iloc[608:989] print table splite = [0] for idx in range(len(table)-1): if compute_date(str(table.iloc[idx]['Date']),str(table.iloc[idx+1]['Date'])) > 3: splite.append(idx) splite.append(len(table)) for idx in range(len(splite)-1): tmp = table[splite[idx]+1:splite[idx+1]-1] X = tmp['X'].values Y = tmp['Y'].values if X !=[] and Y!=[]: lon, lat = map(X, Y) fn = ('segment%d-%d.csv') % (shipMMSI,counter) tmp.to_csv(fn) counter += 1 current_label = ('%s %s %s t\n%s') % (shipMMSI,getNationFlag(shipMMSI),str(tmp['Dun'][0]), str(tmp['Date'][0])) map.plot(lon, lat,c=sorted_names[np.random.randint(100,len(sorted_names))],linestyle='-',marker='D', markersize=1, linewidth=2) plt.text(lon[0], lat[0],current_label) map.scatter(lon[0], lat[0], 60, c='green', marker='*', edgecolors='none', zorder=10) # start map.scatter(lon[-1], lat[-1], 60,c='red',marker='*',edgecolors='none', zorder=10) # end # print X[0],Y[0],X[-1],Y[-1] plt.title('All ShipLine') plt.show()
def map_AllPoint(DirectoryPath): def _DBSCAN(X, Y, color): center_x = [] center_y = [] X = X.reshape(-1, 1) Y = Y.reshape(-1, 1) data = np.hstack((X, Y)) model = DBSCAN(eps=0.015, min_samples=2) model.fit(data) y_hat = model.labels_ core_indices = np.zeros_like(y_hat, dtype=bool) core_indices[model.core_sample_indices_] = True y_unique = np.unique(y_hat) clrs = plt.cm.Spectral(np.linspace(0, 0.8, y_unique.size)) data[:, 0], data[:, 1] = map(data[:, 0], data[:, 1]) for k, clr in zip(y_unique, clrs): cur = (y_hat == k) if k == -1: map.scatter(data[cur, 0], data[cur, 1], s=1, c='black') # nosie continue map.scatter(data[cur, 0], data[cur, 1], s=40, c=color, marker='*') center_x.extend(data[cur, 0]) center_y.extend(data[cur, 1]) map.plot(center_x, center_y, c=sorted_names[np.random.randint(100, len(sorted_names))], linestyle='-', markersize=3, linewidth=1) map = Basemap(projection='mill', lon_0=180, width=12000000, height=9000000) map.drawcoastlines() map.drawparallels(np.arange(-90, 90, 30), labels=[1, 0, 0, 0]) map.drawmeridians(np.arange(map.lonmin, map.lonmax + 30, 60), labels=[0, 0, 0, 1]) map.drawmapboundary(fill_color='white') map.drawcoastlines(linewidth=0.5, color='y') map.drawcountries(color='y') map.drawstates(color='y') map.fillcontinents(color='coral', lake_color='blue') colors = cycle(allcolors.values()) data_paths = [ os.path.join(DirectoryPath, s) for s in os.listdir(DirectoryPath) ] for file, color in zip(data_paths, colors): table = pd.DataFrame(columns=['Date', 'X', 'Y', 'Course']) X = [] Y = [] one_MMSI = [] parse.get_data_DY(file, one_MMSI) x_list = [] y_list = [] date_list = [] course_list = [] speed_list = [] for shipdata in one_MMSI: shipMMSI = shipdata['MMSI'] x = shipdata['DynamicInfo'][0]['Longitude'] # jingdu y = shipdata['DynamicInfo'][0]['Latitude'] # weidu datetime = shipdata['DynamicInfo'][0]['LastTime'] course = shipdata['DynamicInfo'][0]['course'] speed = shipdata['DynamicInfo'][0]['Speed'] date_list.append(datetime) course_list.append(course) speed_list.append(speed) x_list.append(x) y_list.append(y) date = np.array(date_list) x = np.array(x_list) for i in range((x.shape)[0]): if x[i] <= 0: x[i] = 360 + x[i] # correct longtitude y = np.array(y_list) tab = pd.DataFrame(columns=['Date', 'X', 'Y', 'Course', 'Speed']) tab['Date'] = date tab['X'] = x tab['Y'] = y tab['Course'] = np.array(course_list) tab['Speed'] = np.array(speed_list) table = pd.concat([table, tab]) table.set_index('Date', drop=False, append=False, inplace=True, verify_integrity=False) table = table.sort_index() print table # table.to_csv('look.csv') west_east_splite = [] if table.iloc[0]['Course'] > 180: west_east_splite.append(0) else: west_east_splite.append(0) for idx in range(len(table) - 1): if float(table.iloc[idx]['Course']) > 180 and float( table.iloc[idx + 1]['Course']) < 180: west_east_splite.append(idx) # elif float(table.iloc[idx]['Course']) < 180 and float(table.iloc[idx+1]['Course']) > 180: # west2east.append(idx) west_east_splite.append(len(table) - 1) X = table['X'].values Y = table['Y'].values if X != [] and Y != []: _DBSCAN(X, Y, color) lon, lat = map(X, Y) # map.scatter(lon, lat,c='b',marker='D',s=1) plt.title('Japan ShipLine') plt.show()
table.iloc[idx + 1]['Course']) < 180: west_east_splite.append(idx) # elif float(table.iloc[idx]['Course']) < 180 and float(table.iloc[idx+1]['Course']) > 180: # west2east.append(idx) west_east_splite.append(len(table) - 1) X = table['X'].values Y = table['Y'].values if X != [] and Y != []: _DBSCAN(X, Y, color) lon, lat = map(X, Y) # map.scatter(lon, lat,c='b',marker='D',s=1) plt.title('Japan ShipLine') plt.show() if __name__ == "__main__": all_MMSI = [] one_MMSI = [] all_MMSI_DY = [] path = "/media/xxxx/xx/AISProject/Hawaii/tanker" data_paths = [os.path.join(path, s) for s in os.listdir(path)] for file in data_paths: one_MMSI = [] parse.get_data_DY(file, one_MMSI) mapline(one_MMSI)
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import sys import ais_parse as parse import ais_draw as draw if __name__ == "__main__": all_MMSI_cargo = [] all_MMSI_tanker = [] cargo_path = "xxx" tanker_path = "xxx" tmp_cargo = [os.path.join(cargo_path + os.sep, s) for s in os.listdir(cargo_path + os.sep)] tmp_tanker = [os.path.join(tanker_path + os.sep, s) for s in os.listdir(tanker_path + os.sep)] for file in tmp_cargo: parse.get_data_DY(file, all_MMSI_cargo) for file in tmp_tanker: parse.get_data_DY(file, all_MMSI_tanker) # print all_MMSI_cargo,all_MMSI_tanker draw.mapscatter(all_MMSI_cargo,all_MMSI_tanker)