zoukankan      html  css  js  c++  java
  • 利用gis通过shp文件将经纬度解析为城市名 itprobie

    1. android端通过gps解析出所在的城市名,可以使用Geocoder来完成,但是Geocoder函数和android api的版本有关。2.1以上不能用
    2. 通过google map方式如:http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=true可以完成,也是比较简单的一种形式,但是公司网关做了限制,不能访问。
    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。提供一个资料链接:http://www.vividsolutions.com/jts/jtshome.htm 
    • 什么是shp文件:Shapefile文件是美国环境系统研究所(ESRI)所研制的GIS文件系统格式文件,是工业标准的矢量数据文件。 Shapefile将空间特征表中的非拓扑几何对象和属性信息存储在数据集中,特征表中的几何对象存为以坐标点集表示的图形文件—SHP文件,Shapefile文件并不含拓扑(Topological)数据结构。一个Shape文件包括三个文件:一个主文件(*.shp),一个索引文件(*.shx),和一个dBASE(*.dbf)表。主文件是一个直接存取,变长度记录的文件,其中每个记录描述构成一个地理特征(Feature)的所有vertices坐标值。在索引文件中,每条记录包含对应主文件记录距离主文件头开始的偏移量,dBASE表包含SHP文件中每一个Feature的特征属性,表中几何记录和属性数据之间的一一对应关系是基于记录数目的ID。在dBASE文件中的属性记录必须和主文件中的记录顺序是相同的。图形数据和属性数据通过索引号建立一一对应的关系。相应的资料参考链接:http://blog.csdn.net/zapzqc/article/details/3123149
    1. 进入正题:通过shp文件 ,并返回一个集合。
       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  }
    2. 传入经纬度信息解析出城市名。
       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  }
    3. 至此解析工作已完成。进过测试,解析速度还是可以的,如果再优化效率还会提升。
  • 相关阅读:
    Naive Operations HDU6315 (杭电多校2G)
    Baker Vai LightOJ
    LOJ#6278. 数列分块入门 2
    LOJ#6277. 数列分块入门 1
    Picture POJ
    Who Gets the Most Candies? POJ
    Dividing the Path POJ
    Balanced Sequence HDU
    归并排序
    Flying to the Mars
  • 原文地址:https://www.cnblogs.com/guohu/p/15522560.html
Copyright © 2011-2022 走看看