GeoTools应用:提取Shape文件属性列头信息(1)

一、环境准备

装配GeoTools有两种方式,一种是配置maven工程的pom文件(配置方式参考官网),另一种是下载geotools的jar包到本地导入依赖。我采用的是下载jar的方式,下载路径:https://sourceforge.net/projects/geotools/files/

二、实现功能

在实际项目中经常需要提取shape文件的属性列头信息,包含属性的名称和属性类型。下面的Demo代码实现提取列头数据打印到控制台的功能。

补充说明:shape文件的属性数据存储在*.dbf文件中,这个文件可以用excel打开。
这里有shape文件的样例,可下载作为研究测试使用:http://www.naturalearthdata.com/downloads/50m-cultural-vectors/

三、样例代码

1、定义shape文件属性信息模型

package com.elon.model;

import java.io.Serializable;

/**
 * Shape文件字段信息模型。
 * 
 * @author elon
 * @version 2018年6月24日
 */
public class ShapeFieldInfo implements Serializable {

    private static final long serialVersionUID = 6947403344262247581L;

    /**
     * 字段名称
     */
    private String fieldName = "";

    /**
     * 字段类型
     */
    private Class<?> fieldType = null;

    @Override
    public String toString() {
        return "fieldName:" + fieldName + "|fieldType:" + fieldType.getName();
    }

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public Class<?> getFieldType() {
        return fieldType;
    }

    public void setFieldType(Class<?> fieldType) {
        this.fieldType = fieldType;
    }
}

2、提取属性名称和类型信息

package com.elon.shape;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.opengis.feature.type.AttributeDescriptor;

import com.elon.model.ShapeFieldInfo;

/**
 * Shape文件操作公共类。
 * @author elon
 * @version 2018年6月24日
 */
public class ShapeUtils {

    /**
     * 提取shape文件包含的属性字段名称和类型信息。
     * 
     * @param shpFilePath shape文件路径
     * @return 属性信息
     */
    public static List<ShapeFieldInfo> distillShapeFieldInfo(String shpFilePath) {
        List<ShapeFieldInfo> fieldList = new ArrayList<>();
        ShapefileDataStore dataStroe = buildDataStore(shpFilePath);

        try {
            List<AttributeDescriptor> attrList = dataStroe.getFeatureSource().getSchema()
                    .getAttributeDescriptors();
            for (AttributeDescriptor attr : attrList) {
                ShapeFieldInfo field = new ShapeFieldInfo();
                field.setFieldName(attr.getLocalName());
                field.setFieldType(attr.getType().getBinding());
                fieldList.add(field);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            dataStroe.dispose();
        }

        System.out.println("fieldList:" + fieldList);
        return fieldList;
    }

    /**
     * 构建ShapeDataStore对象。
     * @param shpFilePath shape文件路径。
     * @return
     */
    public static ShapefileDataStore buildDataStore(String shpFilePath) {
        ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
        try {
            ShapefileDataStore dataStore = (ShapefileDataStore) factory
                    .createDataStore(new File(shpFilePath).toURI().toURL());
            if (dataStore != null) {
                dataStore.setCharset(Charset.forName("UTF-8"));
            }
            return dataStore;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
很高兴回答你的问题!以下是示例代码,可以在 Android Studio 中使用 GeoTools 读取并显示 shape 文件: 首先,需要在项目的 build.gradle 文件中添加以下依赖项: ``` dependencies { implementation 'org.geotools:gt-shapefile:23.2' implementation 'org.geotools:gt-epsg-hsql:23.2' implementation 'org.geotools:gt-geojson:23.2' } ``` 然后,在你的 Activity 或 Fragment 中,可以使用以下代码读取 shape 文件并将其显示在地图上: ```java import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.geometry.jts.JTS; import org.geotools.map.FeatureLayer; import org.geotools.map.MapContent; import org.geotools.styling.SLD; import org.geotools.styling.Style; import org.locationtech.jts.geom.Envelope; import org.locationtech.jts.geom.Geometry; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import java.io.File; import java.io.IOException; public class MainActivity extends AppCompatActivity { private MapContent mapContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化地图内容 mapContent = new MapContent(); // 读取 shape 文件 File shapeFile = new File("/path/to/shapefile.shp"); ShapefileDataStore dataStore = null; try { dataStore = new ShapefileDataStore(shapeFile.toURI().toURL()); } catch (IOException e) { e.printStackTrace(); } // 获取 feature source 和 feature collection SimpleFeatureSource featureSource = null; SimpleFeatureCollection featureCollection = null; try { featureSource = dataStore.getFeatureSource(); featureCollection = featureSource.getFeatures(); } catch (IOException e) { e.printStackTrace(); } // 获取 feature type 和 geometry type SimpleFeatureType featureType = featureSource.getSchema(); Class<?> geometryType = featureType.getGeometryDescriptor().getType().getBinding(); // 创建 feature layer 和 style FeatureLayer featureLayer = new FeatureLayer(featureCollection, createStyle(geometryType)); mapContent.addLayer(featureLayer); // 设置地图范围 Envelope envelope = featureCollection.getBounds(); mapContent.getViewport().setBounds(envelope); // 获取地图视图并将其添加到布局中 MapView mapView = findViewById(R.id.map_view); mapView.setMapContent(mapContent); } @Override protected void onDestroy() { super.onDestroy(); // 释放地图内容 mapContent.dispose(); } private Style createStyle(Class<?> geometryType) { // 根据几何类型创建默认样式 if (Geometry.class.isAssignableFrom(geometryType)) { return SLD.createSimpleStyle(geometryType); } else { return null; } } } ``` 请注意,上述代码中的 `/path/to/shapefile.shp` 应替换为实际的 shape 文件路径。此外,还需要在布局文件中添加一个 MapView 控件: ```xml <org.geotools.android.MapView android:id="@+id/map_view" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 希望这可以帮助你实现在 Android Studio 中使用 GeoTools 读取并显示 shape 文件
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值