[关闭]
@huyl08 2016-08-27T15:07:17.000000Z 字数 6548 阅读 1528

星迹迷航新手上路(Java版)

Java 星迹迷航 文件读写


http://blog.jobbole.com/88993/

  1. 目前JDK版本为8u102,可前往Oracle Java官网下载最新版本的Java,可根据自身设备操作系统的不同选择相应的安装包版本。本版本的“新手上路”以JDK 8u102作为演示样例
  2. 基本的操作可学习官网新手入门(英语)、Codecademy(英语)、Java程序设计(中文)或其他类似资源。
  3. 完整的Java示例源码可通过该链接下载
  4. 推荐使用IntelliJ IDEA作为集成开发环境。

1. Java安装

下载Windows下最新版本的Java安装包JDK 8u102,双击安装,根据自己需要设置安装位置。

1.1 Java环境变量配置

1.2 确认安装成功

安装成功后,若Java被顺利添加到Path环境变量中,运行命令行cmd.exe(可使用Windows键+R打开“运行”窗口,敲入“cmd”火车),键入以下命令即可看到Java版本信息

  1. java -version

安装成功版本输出类似如下

  1. java version "1.8.0_91"
  2. Java(TM) SE Runtime Environment (build 1.8.0_91-b15)
  3. Java HotSpot(TM) 64-Bit Server VM (build 25.91-b15, mixed mode)

2. Java处理数据

以下为Java源码样例

2.1 坐标辅助类

  1. import java.util.ArrayList;
  2. // 使用Coordinate类记录坐标位置
  3. // 使用静态方法将输入的字符串转化为坐标数组
  4. public class Coordinate{
  5. // 经度坐标
  6. public double x;
  7. // 纬度坐标
  8. public double y;
  9. // Coorindate类的构造方法
  10. public Coordinate(double x, double y){
  11. this.x = x;
  12. this.y = y;
  13. }
  14. // 静态方法
  15. // 将输入的字符串转化为坐标数组
  16. // 参数: geometry_string 记载几何形态的字符串
  17. // 返回值: 记录坐标(Coordinate类)的数组(ArrayList)
  18. public static ArrayList<Coordinate> geometry_parse(String geometry_string){
  19. // 输入字符串为空或者长度为0,返回null值
  20. if( geometry_string == null || geometry_string.length() == 0 )
  21. return null;
  22. // 声明返回值数组
  23. ArrayList<Coordinate> results = new ArrayList<Coordinate>();
  24. // 按“;”分隔字符串
  25. String[] tokens = geometry_string.split(";");
  26. for( String token : tokens ){
  27. // 按":"拆分字符串获得经纬度坐标
  28. String[] values = token.split(":");
  29. double x = Double.parseDouble(values[0]);
  30. double y = Double.parseDouble(values[1]);
  31. results.add(new Coordinate(x, y));
  32. }
  33. return results;
  34. }
  35. }

2.2 将字符串转为坐标数组

  1. import java.util.ArrayList;
  2. // 使用Coordinate类记录坐标位置
  3. // 使用静态方法将输入的字符串转化为坐标数组
  4. public class Coordinate{
  5. // 经度坐标
  6. public double x;
  7. // 纬度坐标
  8. public double y;
  9. // Coorindate类的构造方法
  10. public Coordinate(double x, double y){
  11. this.x = x;
  12. this.y = y;
  13. }
  14. // 静态方法
  15. // 将输入的字符串转化为坐标数组
  16. // 参数: geometry_string 记载几何形态的字符串
  17. // 返回值: 记录坐标(Coordinate类)的数组(ArrayList)
  18. public static ArrayList<Coordinate> geometry_parse(String geometry_string){
  19. // 输入字符串为空或者长度为0,返回null值
  20. if( geometry_string == null || geometry_string.length() == 0 )
  21. return null;
  22. // 声明返回值数组
  23. ArrayList<Coordinate> results = new ArrayList<Coordinate>();
  24. // 按“;”分隔字符串
  25. String[] tokens = geometry_string.split(";")
  26. for( String token : tokens ){
  27. String[] values = token.split(":")
  28. double x = Double.parseDouble(values[0]);
  29. double y = Double.parseDouble(values[1]);
  30. results.add(new Coordinate(x, y));
  31. }
  32. return results;
  33. }
  34. }

2.3 读取电子地图文件

  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.StringTokenizer;
  8. public class LinkReader{
  9. // 定义静态类Link,用于存储路段信息
  10. public static class Link{
  11. public int link_id;
  12. public int from_node;
  13. public int to_node;
  14. public double link_length;
  15. public int link_class;
  16. // 坐标序列
  17. public ArrayList<Coordinate> coordinates;
  18. // 将坐标变量转化为字符串
  19. public String coordinateToString(){
  20. if( coordinates == null || coordinates.size() == 0 )
  21. return "";
  22. String geomStr = "";
  23. for( Coordinate coordinate : coordinates ){
  24. geomStr += String.format("%.6f:%.6f\t", coordinate.x, coordinate.y);
  25. }
  26. return geomStr;
  27. }
  28. }
  29. // 静态读文件函数
  30. // 参数: file_path为输入文件路径
  31. // 返回值: 返回路段属性字典(HashMap)
  32. public static HashMap<Integer, Link> readLink(String file_path){
  33. try{
  34. // 生成返回的HashMap字典对象
  35. HashMap<Integer, Link> results = new HashMap<Integer, Link>();
  36. // 创建文件输入流,使用BufferedReader按行读取
  37. BufferedReader reader = new BufferedReader(new FileReader(new File(file_path)));
  38. // 声明存储文件行的临时变量
  39. String line = null;
  40. // 读入行到临时变量line中, 在不为空的情况下将其转化为Link对象并存储
  41. while( ( line = reader.readLine() ) != null ){
  42. StringTokenizer tokenizer = new StringTokenizer(line.trim(), ",");
  43. Link link = new Link();
  44. link.link_id = Integer.parseInt(tokenizer.nextToken());
  45. link.from_node = Integer.parseInt(tokenizer.nextToken());
  46. link.to_node = Integer.parseInt(tokenizer.nextToken());
  47. link.link_length = Double.parseDouble(tokenizer.nextToken());
  48. link.link_class = Integer.parseInt(tokenizer.nextToken());
  49. link.coordinates = Coordinate.geometry_parse(tokenizer.nextToken());
  50. results.put(link.link_id, link);
  51. }
  52. // 关闭文件流
  53. reader.close();
  54. // 返回结果
  55. return results;
  56. }catch(IOException e){
  57. e.printStackTrace();
  58. return null;
  59. }
  60. }
  61. }

2.4 Java操作GPS

  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. public class GPS {
  5. // 车辆编号
  6. public String vid;
  7. // 记录时间戳
  8. public Date datetime;
  9. // 经度
  10. public double x;
  11. // 纬度
  12. public double y;
  13. // 速度,单位为km/h
  14. public double speed;
  15. // 瞬时方向角,单位为度,正北为0,顺时针为正方向
  16. public double direction;
  17. // 日期时间(Date)和字符串的转化辅助类
  18. public static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  19. public static SimpleDateFormat sdf_str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  20. // 按读入的格式化字符串生成GPS实例
  21. public GPS(String line) throws ParseException {
  22. String[] tokens = line.trim().split(",");
  23. this.vid = new String(tokens[0]);
  24. this.datetime = sdf.parse(tokens[1]);
  25. this.x = Double.parseDouble(tokens[2]);
  26. this.y = Double.parseDouble(tokens[3]);
  27. this.speed = Double.parseDouble(tokens[4]);
  28. this.direction = Double.parseDouble(tokens[5]);
  29. }
  30. public String toString(){
  31. return String.format("%s\t%s\t(%.6f, %.6f)", this.vid, sdf_str.format(this.datetime), this.x, this.y);
  32. }
  33. // 求和另一条记录之间的时间差, 以秒为单位
  34. public double timeDifference(GPS another){
  35. return ((double)( another.datetime.getTime() - this.datetime.getTime() ) / 1000.0 );
  36. }
  37. // 求和另一条记录之间的距离
  38. // 注意:实际计算中需要做经纬度到以米为单位的投影转换
  39. public double distance(GPS another){
  40. double dx = another.x - this.x;
  41. double dy = another.y - this.y;
  42. return Math.sqrt( dx * dx + dy * dy );
  43. }
  44. }

2.5 测试主类

  1. import java.text.ParseException;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. public class StarTrek {
  5. public static void main(String[] args) throws ParseException {
  6. GPS gps_a = new GPS("13381083463,20160701120147,116.387472,39.889589,26.00,266");
  7. GPS gps_b = new GPS("13311492120,20160701120144,116.355589,39.993436,10.00,260");
  8. System.out.println(String.format("GPS A is %s", gps_a.toString()));
  9. System.out.println(String.format("GPS B is %s", gps_b.toString()));
  10. System.out.println(String.format("Disatance between A and B is %.5f degrees", gps_a.distance(gps_b)));
  11. System.out.println(String.format("Time difference between A and B is %.0f seconds", gps_a.timeDifference(gps_b)));
  12. HashMap<Integer, LinkReader.Link> links = LinkReader.readLink("gis.txt");
  13. System.out.println("\nLinkID\tLinkClass\tFromNode\tToNode\tLinkLength\tGeometry");
  14. Iterator<Integer> iterator = links.keySet().iterator();
  15. while( iterator.hasNext() ){
  16. Integer linkid = iterator.next();
  17. LinkReader.Link link = links.get(linkid);
  18. System.out.println(String.format("%d\t%d\t%d\t%d\t%.1f\t%s", linkid, link.link_class,
  19. link.from_node, link.to_node, link.link_length, link.coordinateToString()));
  20. }
  21. }
  22. }

附录A 电子地图数据样例

  1. 1,3,4,280.3,2,115.488988:39.917917;115.488997:39.917938;115.489045:39.918061;115.489219:39.918316;115.489335:39.918486;115.489366:39.918540;115.489382:39.918612;115.489385:39.918672;115.489373:39.918713
  2. 3,7,8,173.2,2,115.381547:39.950776;115.381786:39.950461;115.381848:39.950355;115.381887:39.950260;115.381913:39.950163

附录B 浮动车数据样例

  1. 57952495354,20160701120100,116.372966,39.908912,26.00,262
  2. 13381083463,20160701120147,116.387472,39.889589,26.00,266
  3. 13311492120,20160701120144,116.355589,39.993436,10.00,260
  4. 13439610594,20160701120147,116.394988,39.940625,0.00,0
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注