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

读取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);  
} 
如果有什么问题请留言

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值