利用gis通过shp文件将经纬度解析为城市名

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

利⽤gis通过shp⽂件将经纬度解析为城市名
1. android端通过gps解析出所在的城市名,可以使⽤Geocoder来完成,但是Geocoder函数和android api的版本有关。

2.1以上不能⽤
2. 通过google map⽅式如:可以完成,也是⽐较简单的⼀种形式,但是公司⽹关做了限制,不能访问。

3. 没有办法,只能通过gis解析城市名。

4. 先来了解⼀下什么是gis:即地理信息系统(Geographic Information System),下⾯分点来介绍下所⽤到的开源的java GIS系统。

第⼀个⽤到的是Geotools,他是⼀个开源的Java GIS⼯具包,可利⽤它来开发符合标准的地理信息系统。

Geotools提供了OGC(Open Geospatial Consortium)规范的⼀个实现来作为他们的开发。

第⼆个⽤到的是JTS,他是加拿⼤的 Vivid Solutions 做的⼀套开放源码的 Java API。

它提供了⼀套空间数据操作的核⼼算法,为在兼容OGC标准的空间对象模型中进⾏基础的⼏何操作提供2D空间谓词API。

提供⼀个资料链接:
什么是shp⽂件:Shapefile⽂件是美国环境系统研究所(ESRI)所研制的GIS⽂件系统格式⽂件,是⼯业标准的⽮量数据⽂件。

Shapefile将空间特征表中的⾮拓扑⼏何对象和属性信息存储在数据集中,特征表中的⼏何对象存为以坐标点集表⽰的图形⽂件—SHP ⽂件,Shapefile⽂件并不含拓扑(Topological)数据结构。

⼀个Shape⽂件包括三个⽂件:⼀个主⽂件(*.shp),⼀个索引⽂件(*.shx),和⼀个dBASE(*.dbf)表。

主⽂件是⼀个直接存取,变长度记录的⽂件,其中每个记录描述构成⼀个地理特征(Feature)的所有vertices坐标值。

在索引⽂件中,每条记录包含对应主⽂件记录距离主⽂件头开始的偏移量,dBASE表包含SHP⽂件中每⼀个Feature的特征属性,表中⼏何记录和属性数据之间的⼀⼀对应关系是基于记录数⽬的ID。

在dBASE⽂件中的属性记录必须和主⽂件中的记录顺序是相同的。

图形数据和属性数据通过索引号建⽴⼀⼀对应的关系。

相应的资料参考链接:
1. 进⼊正题:通过shp⽂件 ,并返回⼀个集合。

1// 读取shp⽂件,返回所有⽂件信息
2public static FeatureIterator<SimpleFeature> readSHP()
3 {
4 ShapefileDataStore shpDataStore = null;
5long time1 = System.currentTimeMillis();
6try
7 {
8// shp⽂件路径
9 shpDataStore = new ShapefileDataStore(new File("E:\\geo\\geo\\region.shp").toURI().toURL());
10 shpDataStore.setStringCharset(Charset.forName("GBK"));
11// ⽂件名称
12 String typeName = shpDataStore.getTypeNames()[0];
13 FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;
14 featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) shpDataStore.getFeatureSource(typeName);
15 FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();
16 itertor = result.features();
17 }
18catch (Exception e)
19 {
20 e.printStackTrace();
21 }
22 System.out.println(System.currentTimeMillis() - time1 + "读取shp⽂件时间");
23return itertor;
24 }
2. 传⼊经纬度信息解析出城市名。

1// 根据个gps坐标获取对应城市
2public static String readInfo(String poingX, String pointY)
3 {
4 StringBuffer mBuffer = null;
5 String mCityName = "";
6try
7 {
8if (null == itertor)
9 {
10 itertor = readSHP();
11 }
12 mBuffer = new StringBuffer();
13// 迭代iterator
14while (itertor.hasNext())
15 {
16 SimpleFeature feature = itertor.next();
17 Collection<Property> p = feature.getProperties();
18 Iterator<Property> it = p.iterator();
19while (it.hasNext())
20 {
21 String value = null;
22 Property pro = it.next();
23 value = pro.getValue().toString();
24 mBuffer.append(value + "\r\n");
25 }
26 count++;
27 mList.add(mBuffer.toString());
28 mBuffer.delete(0, mBuffer.length());
29 }
30 itertor.close();
31 String data = null;
32 String[] arr = null;
33// 如果缓存mGpsBuffer为空,则从新到所有集合中循环。

34if (null != mGpsBuffer)
35 {
36 WKTReader reader = new WKTReader(geometryFactory);
37 String point1 = "POINT(" + poingX + " " + pointY + ")";
38 Point point = (Point) reader.read(point1);
39 MultiPolygon multiPolygon = (MultiPolygon) reader.read(mGpsBuffer[0]);
40// 传过来的gps经纬度坐标,为⼀个点
41 Geometry geometry1 = geometryFactory.createGeometry(point);
42// 区域内所有点围城的⼀个多边形
43 Geometry geometry2 = geometryFactory.createGeometry(multiPolygon);
44// 如果该点包含在多边形⾥⾯则解析出城市名
45if (geometry2.contains(geometry1))
46 {
47 mCityName = mGpsBuffer[1];
48 System.out.println("cityname1 = " + mGpsBuffer[1]);
49return mCityName;
50 }
51 }
52//循环所有点集合,判断点是否在其中的多边形⾥⾯。

53for (int i = 0; i < mList.size(); i++)
54 {
55 data = mList.get(i);
56 arr = data.split("\r\n");
57try
58 {
59 mGpsBuffer = new String[arr.length];
60 WKTReader reader = new WKTReader(geometryFactory);
61 String point1 = "POINT(" + poingX + " " + pointY + ")";
62 Point point = (Point) reader.read(point1);
63 MultiPolygon multiPolygon = (MultiPolygon) reader.read(arr[0]);
64 Geometry geometry1 = geometryFactory.createGeometry(point);
65 Geometry geometry2 = geometryFactory.createGeometry(multiPolygon);
66if (geometry2.contains(geometry1))
67 {
68 mCityName = arr[1];
69//将多边形的信息存⼊缓存中。

70 mGpsBuffer = arr;
71 System.out.println("cityname = " + arr[1]);
72break;
73 }
74 }
75catch (Exception e)
76 {
77 e.printStackTrace();
78 }
79 }
80 }
81catch (Exception e)
82 {
83 e.printStackTrace();
84// TODO: handle exception
85 }
86return mCityName;
87 }
3. ⾄此解析⼯作已完成。

进过测试,解析速度还是可以的,如果再优化效率还会提升。

相关文档
最新文档