- The android side parses the city name through gps, which can be completed by Geocoder, but the Geocoder function is related to the version of android api. 2.1 the above cannot be used
- Through google map, for example: http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=true It can be completed, which is also a relatively simple form, but the company gateway is limited and cannot be accessed.
- There is no way but to resolve the city name through gis.
- Let's first understand what gis is: geographic information system. The following points will introduce the open source java GIS system used.
- The first one is geotools, an open source Java GIS toolkit, which can be used to develop standard GIS. Geotools provides an implementation of OGC(Open Geospatial Consortium) specification as their development.
- The second one uses JTS, which is an open source Java API developed by Canadian video solutions. It provides a set of core algorithms for spatial data operations and provides a 2D spatial predicate API for basic geometric operations in OGC compliant spatial object models. A data link is provided: http://www.vividsolutions.com/jts/jtshome.htm
- What is a SHP file: the Shapefile file is the Environmental Systems Institute (ESRI) The developed GIS file system format file is an industrial standard vector data file. Shapefile stores the non Topological geometric objects and attribute information in the spatial Feature table in the data set. The geometric objects in the Feature table are stored as a graphic file represented by the coordinate point set - SHP file. The Shapefile file does not contain topology Data structure. A Shape file consists of three files: a main file (*. shp), an index file (*. shx), and a dBASE(*.dbf) table. The main file is a file with direct access and variable length records, in which each record description constitutes a geographic Feature In the index file, each record contains the offset of the corresponding master file record from the beginning of the master file header. The dBASE table contains the Feature attributes of each Feature in the SHP file. The one-to-one correspondence between geometric records and attribute data in the table is based on the ID of the number of records. The attribute records in the dBASE file must be in the same order as the records in the master file The same. Graphic data and attribute data establish one-to-one correspondence through index numbers. Corresponding reference links: http://blog.csdn.net/zapzqc/article/details/3123149
- Get to the point: through shp file , And returns a collection.
1 // read shp file,Return all file information 2 public static FeatureIterator<SimpleFeature> readSHP() 3 { 4 ShapefileDataStore shpDataStore = null; 5 long time1 = System.currentTimeMillis(); 6 try 7 { 8 // shp File path 9 shpDataStore = new ShapefileDataStore(new File("E:\\geo\\geo\\region.shp").toURI().toURL()); 10 shpDataStore.setStringCharset(Charset.forName("GBK")); 11 // File name 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 + "read shp File time"); 23 return itertor; 24 }
- The city name is parsed from the longitude and latitude information.
1 // According to gps Coordinate acquisition corresponding city 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 // iteration 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 // If cache mGpsBuffer If it is empty, it loops from new to all sets. 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 // It's coming gps Longitude and latitude coordinates are one point 41 Geometry geometry1 = geometryFactory.createGeometry(point); 42 // A polygon surrounded by all points in the area 43 Geometry geometry2 = geometryFactory.createGeometry(multiPolygon); 44 // If the point is included in the polygon, the city name is resolved 45 if (geometry2.contains(geometry1)) 46 { 47 mCityName = mGpsBuffer[1]; 48 System.out.println("cityname1 = " + mGpsBuffer[1]); 49 return mCityName; 50 } 51 } 52 //Loop all point sets to determine whether the points are in the polygon. 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 //Store polygon information in the cache. 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 }
- So far, the parsing work has been completed. After testing, the parsing speed is OK, and the efficiency will be improved if re optimized.