1 // 读取shp文件,返回所有文件信息 2 public static FeatureIterator<SimpleFeature> readSHP() 3 { 4 ShapefileDataStore shpDataStore = null; 5 long time1 = System.currentTimeMillis(); 6 try 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 } 18 catch (Exception e) 19 { 20 e.printStackTrace(); 21 } 22 System.out.println(System.currentTimeMillis() - time1 + "读取shp文件时间"); 23 return itertor; 24 }
1 // 根据个gps坐标获取对应城市 2 public static String readInfo(String poingX, String pointY) 3 { 4 StringBuffer mBuffer = null; 5 String mCityName = ""; 6 try 7 { 8 if (null == itertor) 9 { 10 itertor = readSHP(); 11 } 12 mBuffer = new StringBuffer(); 13 // 迭代iterator 14 while (itertor.hasNext()) 15 { 16 SimpleFeature feature = itertor.next(); 17 Collection<Property> p = feature.getProperties(); 18 Iterator<Property> it = p.iterator(); 19 while (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为空,则从新到所有集合中循环。 34 if (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 // 如果该点包含在多边形里面则解析出城市名 45 if (geometry2.contains(geometry1)) 46 { 47 mCityName = mGpsBuffer[1]; 48 System.out.println("cityname1 = " + mGpsBuffer[1]); 49 return mCityName; 50 } 51 } 52 //循环所有点集合,判断点是否在其中的多边形里面。 53 for (int i = 0; i < mList.size(); i++) 54 { 55 data = mList.get(i); 56 arr = data.split("\r\n"); 57 try 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); 66 if (geometry2.contains(geometry1)) 67 { 68 mCityName = arr[1]; 69 //将多边形的信息存入缓存中。 70 mGpsBuffer = arr; 71 System.out.println("cityname = " + arr[1]); 72 break; 73 } 74 } 75 catch (Exception e) 76 { 77 e.printStackTrace(); 78 } 79 } 80 } 81 catch (Exception e) 82 { 83 e.printStackTrace(); 84 // TODO: handle exception 85 } 86 return mCityName; 87 }