1、GeoTools下載
GeoTools是一個Java GIS工具包,用於處理和映射地理空間數據的工具包。
下載地址:https://sourceforge.net/projects/geotools/files/
我下載的是最新的版本18.4版本,下載完成後首先解壓一下備用。
2、測試數據
測試數據保存在txt文本文件中,數據如下所示:爲一個航班數據,字段名依次爲:id,number,oAirport,dAirport,routes,oProvince,oCity,dProvince,dCity,oPeriod,dPeriod,oTime,dTime,duration,oLongitude,oLatitude,dLongitude,dLatitude,type,company,flightNo,day,distance
8664 13807 上海浦東國際機場 西安咸陽國際機場 上海到西安 上海 上海 陝西 西安 14點-15點 16點-17點 14時10分 16時50分 160 121.7987735 31.15202989 108.7553259 34.44126806 JET 東方航空 MU2158 星期三 1271751.617
8678 13825 上海浦東國際機場 大連周水子國際機場 上海到大連 上海 上海 遼寧 大連 14點-15點 16點-17點 14時10分 16時15分 125 121.7987735 31.15202989 121.5390091 38.96171313 325 東方航空 MU5611 星期三 868718.2935
8754 13924 上海浦東國際機場 遵義機場 上海到遵義 上海 上海 貴州 遵義 14點-15點 17點-18點 14時20分 17時20分 180 121.7987735 31.15202989 107.2497459 27.80320661 JET 東方航空 MU5383 星期三 1455480.579
8794 13996 上海浦東國際機場 成都雙流國際機場 上海到成都 上海 上海 四川 成都 14點-15點 17點-18點 14時25分 17時50分 205 121.7987735 31.15202989 103.9564263 30.58167571 JET 中國國航 CA4514 星期三 1702316.086
8813 14023 上海浦東國際機場 北京首都國際機場 上海到北京 上海 上海 北京 北京 14點-15點 16點-17點 14時25分 16時50分 145 121.7987735 31.15202989 116.6089312 40.05391563 321 東方航空 MU272 星期三 1094885.913
8913 14176 上海浦東國際機場 深圳寶安國際機場 上海到深圳 上海 上海 廣東 深圳 14點-15點 17點-18點 14時35分 17時15分 160 121.7987735 31.15202989 113.8085011 22.62780861 321 東方航空 MU3453 星期三 1234535.181
9015 14329 上海浦東國際機場 青島流亭國際機場 上海到青島 上海 上海 山東 青島 14點-15點 16點-17點 14時50分 16時25分 95 121.7987735 31.15202989 120.3808789 36.26650186 320 吉祥航空 HO1115 星期三 583608.0183
9066 14410 上海浦東國際機場 延吉朝陽川國際機場 上海到延吉 上海 上海 吉林 延吉 14點-15點 18點-19點 14時55分 18時00分 185 121.7987735 31.15202989 129.4481272 42.88948575 320 南方航空 CZ9507 星期三 1469684.535
9069 14419 上海浦東國際機場 煙臺蓬萊國際機場 上海到煙臺 上海 上海 山東 煙臺 14點-15點 16點-17點 14時55分 16時45分 110 121.7987735 31.15202989 120.9912577 37.65565652 320 東方航空 MU5545 星期三 726945.8553
9201 14671 上海浦東國際機場 海口美蘭國際機場 上海到海口 上海 上海 海南 海口 15點-16點 18點-19點 15時00分 18時05分 185 121.7987735 31.15202989 110.4552312 19.94215665 JET 深圳航空 ZH1973 星期三 1685540.988
9207 14680 上海浦東國際機場 長春龍嘉國際機場 上海到長春 上海 上海 吉林 長春 15點-16點 18點-19點 15時00分 18時00分 180 121.7987735 31.15202989 125.6939407 43.9918223 JET 深圳航空 ZH1975 星期三 1467938.473
9294 14842 上海浦東國際機場 瀋陽桃仙國際機場 上海到瀋陽 上海 上海 遼寧 瀋陽 15點-16點 17點-18點 15時05分 17時40分 155 121.7987735 31.15202989 123.4880401 41.63506482 320 深圳航空 ZH5045 星期三 1175354.885
9421 15013 上海浦東國際機場 宜賓菜壩機場 上海到宜賓 上海 上海 四川 宜賓 15點-16點 18點-19點 15時25分 18時45分 200 121.7987735 31.15202989 104.5556926 28.79547272 319 南方航空 CZ9657 星期三 1679664.253
9469 15076 上海浦東國際機場 哈爾濱太平國際機場 上海到哈爾濱 上海 上海 黑龍江 哈爾濱 15點-16點 18點-19點 15時30分 18時30分 180 121.7987735 31.15202989 126.2334976 45.62144654 JET 吉祥航空 HO1167 星期三 1654001.279
9476 15085 上海浦東國際機場 重慶江北國際機場 上海到重慶 上海 上海 重慶 重慶 15點-16點 18點-19點 15時30分 18時25分 175 121.7987735 31.15202989 106.6345554 29.72071091 738 海南航空 HU7151 星期三 1461337.109
9516 15139 上海浦東國際機場 桂林兩江國際機場 上海到桂林 上海 上海 廣西壯族自治區 桂林 15點-16點 18點-19點 15時35分 18時20分 165 121.7987735 31.15202989 110.0517184 25.21508965 JET 上海航空 FM9377 星期三 1325956.606
9535 15166 上海浦東國際機場 武漢天河國際機場 上海到武漢 上海 上海 湖北 武漢 15點-16點 17點-18點 15時35分 17時40分 125 121.7987735 31.15202989 114.2084549 30.7656609 JET 東方航空 MU518 星期三 724897.0712
9581 15247 上海浦東國際機場 柳州白蓮機場 上海到柳州 上海 上海 廣西壯族自治區 柳州 15點-16點 18點-19點 15時40分 18時15分 155 121.7987735 31.15202989 109.4034117 24.2031901 JET 東方航空 MU5207 星期三 1443008.195
9605 15283 上海浦東國際機場 烏魯木齊地窩堡國際機場 上海到烏魯木齊 上海 上海 新疆維吾爾自治區 烏魯木齊 15點-16點 22點-23點 15時45分 22時50分 425 121.7987735 31.15202989 87.47511044 43.90202244 738 海南航空 HU7790 星期三 3311030.678
9639 15328 上海浦東國際機場 長沙黃花國際機場 上海到長沙 上海 上海 湖南 長沙 15點-16點 17點-18點 15時45分 17時50分 125 121.7987735 31.15202989 113.2146315 28.19017886 73H 上海航空 FM9399 星期三 892011.7518
9646 15337 上海浦東國際機場 呼和浩特白塔國際機場 上海到呼和浩特 上海 上海 內蒙古自治區 呼和浩特 15點-16點 18點-19點 15時45分 18時30分 165 121.7987735 31.15202989 111.816061 40.85288036 738 海南航空 HU7790 星期三 1401527.839
9680 15382 上海浦東國際機場 珠海機場 上海到珠海 上海 上海 廣東 珠海 15點-16點 18點-19點 15時45分 18時30分 165 121.7987735 31.15202989 113.3696305 22.01192578 JET 東方航空 MU9057 星期三 1316302.511
9694 15400 上海浦東國際機場 昆明長水國際機場 上海到昆明 上海 上海 雲南 昆明 15點-16點 20點-21點 15時50分 20時40分 290 121.7987735 31.15202989 102.9288901 25.10164774 320 東方航空 MU5463 星期三 1965686.521
9701 15409 上海浦東國際機場 銀川河東機場 上海到銀川 上海 上海 寧夏回族自治區 銀川 15點-16點 19點-20點 15時50分 19時10分 200 121.7987735 31.15202989 106.3854625 38.32349892 320 吉祥航空 HO1336 星期三 1615331.203
3、創建Java項目
1.在Eclipse中創建一個名爲Txt2Shp
的項目,新建一個包,我這裏名爲com.demo.conversion
。
2.將下載好的解壓好的GeoTools工具包拷貝到新建的項目當中,右鍵項目–>Build Path–>Configure Build Path,如下圖所示對話框,選中工具包中jar包將其添加到項目中,並點擊ok。
3.在包裏新建一個名爲SaveShape
類,代碼如下:
public class SaveShape {
public void SaveLines(String filePath, List<String> pointData) {
int count=0;
try {
// 創建shape對象
File file = new File(filePath);
Map<String, Serializable> params = new HashMap<>();
params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
// 定義圖形信息和屬性信息
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
tb.setCRS(DefaultGeographicCRS.WGS84);
tb.setName("shapefile");
tb.add("the_geom", LineString.class);
tb.add("id", Integer.class);
tb.add("number", Integer.class);
tb.add("oAirport", String.class);
tb.add("dAirport", String.class);
tb.add("routes", String.class);
tb.add("oProvince", String.class);
tb.add("oCity", String.class);
tb.add("dProvince", String.class);
tb.add("dCity", String.class);
tb.add("oPeriod", String.class);
tb.add("dPeriod", String.class);
tb.add("oTime", String.class);
tb.add("dTime", String.class);
tb.add("duration", Integer.class);
tb.add("oLongitude", Double.class);
tb.add("oLatitude", Double.class);
tb.add("dLongitude", Double.class);
tb.add("dLatitude", Double.class);
tb.add("type", String.class);
tb.add("company", String.class);
tb.add("flightNo", String.class);
tb.add("day", String.class);
tb.add("distance", Double.class);
ds.createSchema(tb.buildFeatureType());
ds.setCharset(Charset.forName("GBK"));
// 設置Writer
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0],
Transaction.AUTO_COMMIT);
for (int i = 0; i< pointData.size(); i++) {
SimpleFeature feature = writer.next();
String line = pointData.get(i);
String[] split = line.split(" ");
Integer id = Integer.valueOf(split[0]);
Integer number = Integer.valueOf(split[1]);
String oAirport = split[2];
String dAirport = split[3];
String routes = split[4];
String oProvince = split[5];
String oCity = split[6];
String dProvince = split[7];
String dCity = split[8];
String oPeriod = split[9];
String dPeriod = split[10];
String oTime = split[11];
String dTime = split[12];
Integer duration = Integer.valueOf(split[13]);
Double oLongitude = Double.valueOf(split[14]);
Double oLatitude = Double.valueOf(split[15]);
Double dLongitude = Double.valueOf(split[16]);
Double dLatitude = Double.valueOf(split[17]);
String type = split[18];
String company = split[19];
String flightNo = split[20];
String day = split[21];
Double distance = Double.valueOf(split[22]);
Coordinate[] pp = new Coordinate[] { new Coordinate(oLongitude, oLatitude),
new Coordinate(dLongitude, dLatitude) };
feature.setAttribute("the_geom", new GeometryFactory().createLineString(pp));
feature.setAttribute("id", id);
feature.setAttribute("number", number);
feature.setAttribute("oAirport", oAirport);
feature.setAttribute("dAirport", dAirport);
feature.setAttribute("routes", routes);
feature.setAttribute("oProvince", oProvince);
feature.setAttribute("oCity", oCity);
feature.setAttribute("dProvince", dProvince);
feature.setAttribute("dCity", dCity);
feature.setAttribute("oPeriod", oPeriod);
feature.setAttribute("dPeriod", dPeriod);
feature.setAttribute("oTime", oTime);
feature.setAttribute("dTime", dTime);
feature.setAttribute("duration", duration);
feature.setAttribute("oLongitude", oLongitude);
feature.setAttribute("oLatitude", oLatitude);
feature.setAttribute("dLongitude", dLongitude);
feature.setAttribute("dLatitude", dLatitude);
feature.setAttribute("type", type);
feature.setAttribute("company", company);
feature.setAttribute("flightNo", flightNo);
feature.setAttribute("day", day);
feature.setAttribute("distance", distance);
count++;
System.out.println("第" + count + "條success!");
}
System.out.println("總共" + count + "條success!");
writer.write();
writer.close();
ds.dispose();
System.out.println("******數據轉線*******");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4.新建一個名爲ReadSaveLines
的類,用於讀取文件並藉助上面的方法來完成數據的轉換操作,代碼如下:
public class ReadSaveLines {
public static void main(String[] args)throws IOException {
//配置輸入輸出路徑
File file = new File("D:\\data2015\\China Airplane Routes.txt");
String shapepath="D:\\data2015\\China Airplane Routes.shp";
try {
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
@SuppressWarnings("resource")
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line ="";
//生成線集合
int number=0;
List<String> pdata = new ArrayList<>();
while((line = bufferedReader.readLine())!=null){
pdata.add(number, line);
number=number+1;
}
System.out.println("*********讀取數據完畢!*********");
//保存爲shapefile
SaveShape ss=new SaveShape();
ss.SaveLines(shapepath, pdata);
System.out.println("***********finish!***********");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4、執行代碼並查看結果
1.執行代碼後在控制檯顯示完成數據轉換操作
2.將文件加載到ArcMap中查看,如圖所示
5、出現中文亂碼後的解決辦法
ArcGIS10.2以上貌似都會出現此問題,解決辦法是在cmd或PowerShell(windows10中PowerShell逐漸上位並取代cmd)中執行以下命令即可,我這裏是10.5,大家根據自己軟件的版本更改命令中的版本號即可。reg add HKEY_CURRENT_USER\Software\ESRI\Desktop10.5\Common\CodePage /v dbfDefault /t REG_SZ /d 936 /f
具體參考Esri官網的解釋:https://support.esri.com/zh-cn/technical-article/000013192
關於Shapefile字符截斷問題,請參見:https://blog.csdn.net/qingjianduoyun/article/details/72935395