android开发——使用java读取.shp(shapefile)矢量文件

来源:互联网 发布:spyder tensorflow 编辑:程序博客网 时间:2024/05/19 17:09

读取shapefile文件对于地图软件开发的人员来说是至关重要的,所以在这里给大家分享一下在android studio平台下如何读取shapefile空间数据文件;

闲话不多说直接上代码(绝对可以读取):

(注意:这段代码仅仅是读取 polyline的,其他集合类型的读取原理基本一致,参照就可以)

(代码说明:ShpPolyline 是自定义的一个model类

String shpfilepath =  path;//.shp文件的全路径path = path.replace("shp","shx");String shxfilepath = path;try{    File fs = new File(shxfilepath);    long BytesSum = fs.length();    int shapecount = (int)(BytesSum - 100) / 8;//计算.shp中存储的信息条目个数    System.out.print(shapecount+"个");    if (shxfilepath == "")    {        return;    }
***************************************************//读取头记录
    fs = new File(shpfilepath);    DataInputStream  BinaryFile = new DataInputStream(new FileInputStream(fs));    for(int i=0;i<7;i++){    System.out.println(BinaryFile.readInt()+"");    }    BinaryFile.read(buf);    System.out.println(bytesToInt(buf,0)+"nei");
****************************************************    BinaryFile.read(buf);    int shapetype = bytesToInt(buf,0);    System.out.println(shapetype+"");//输出几何类型(有0,1,3,5,8....)(不懂百度一下说明很详细)    BinaryFile.read(buf2);    BinaryFile.read(buf2);    BinaryFile.read(buf2);    BinaryFile.read(buf2);    BinaryFile.read(new byte[32]);    double x, y;    int partcount;    int pointcount;    switch (shapetype)//根据几何类型进行读取    {        case 1://single point        break;        case 8://multi points layer            break;        case 3://Polyline layer           System.out.println("111111111111111111111111111111111111");            for (int i = 0; i < shapecount; i++)            {                ShpPolyline shapePolyline = new ShpPolyline();                BinaryFile.read(new byte[12]);                BinaryFile.read(buf2);                BinaryFile.read(buf2);                BinaryFile.read(buf2);                BinaryFile.read(buf2);                                BinaryFile.read(buf);                partcount = bytesToInt(buf,0);                BinaryFile.read(buf);                pointcount = bytesToInt(buf,0);                                System.out.println(pointcount+"6666666");                System.out.println(partcount+"8888888");                int[] parts = new int[partcount];                int[] partspos = new int[partcount];                double[] xpoints = new double[pointcount];                double[] ypoints = new double[pointcount];                double[] zpoints = new double[pointcount];                for (int j = 0; j < partcount; j++)                {                   BinaryFile.read(buf);                    parts[j] = bytesToInt(buf,0);                    System.out.println(parts[j]);                }                for (int j = 0; j < pointcount; j++)                {                   BinaryFile.read(buf2);                    x = bytes2Double(buf2);                    BinaryFile.read(buf2);                    y = bytes2Double(buf2);                    System.out.println(x+"woqu");                    System.out.println(y+"woqu1");                    xpoints[j] = x;                    ypoints[j] = y;                    zpoints[j] = 0;                }                if (pointcount > 1)                {                    shapePolyline.OID = i;                    shapePolyline.XPOINTS = xpoints;                    shapePolyline.YPOINTS = ypoints;                }                shpPolylineList.add(shapePolyline);            }            BinaryFile.close();        break;
数据的转化高地位变化:
//int数据的高地位变化
public static int bytesToInt(byte[] src, int offset) {      int value;        value = (int) ((src[offset] & 0xFF)               | ((src[offset+1] & 0xFF)<<8)               | ((src[offset+2] & 0xFF)<<16)               | ((src[offset+3] & 0xFF)<<24));      return value;  }  
//double数据的转化
public static double bytes2Double(byte[] arr) {      long value = 0;      for (int i = 0; i < 8; i++) {          value |= ((long) (arr[i] & 0xff)) << (8 * i);      }      return Double.longBitsToDouble(value);  } 
如果有什么问题请留言

原创粉丝点击