南归北隐 2019-07-05 19:26 采纳率: 42.9%
浏览 786

Geotools 创建shape文件报错,无法设置中文属性

    feature.setAttribute(0, polygon.getCode());
    这个属性名乱码,如果设置成中文就报错
    eg:     feature.setAttribute("图斑编码", polygon.getCode());
package com.kero99.utils;
/**
 * 创建shape文件
 * @author ygc
 *
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.geotools.data.FeatureWriter;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.shapefile.dbf.DbaseFileHeader;
import org.geotools.data.shapefile.dbf.DbaseFileReader;
import org.geotools.data.shapefile.files.ShpFiles;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.IllegalAttributeException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.feature.type.Types;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.geojson.geom.GeometryJSON;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.util.Converters;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;

import com.kero99.patterns.IShape;
import com.kero99.pojo.Zdesignpolygon;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.MultiPolygon;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * Geotools文档:http://docs.geotools.org/latest/javadocs/
 * 创建shape文件
 * @author ygc
 *
 */
public class createShapeFile {
    public static void main(String[] args) throws IOException {
        //创建文件路径
        String path="C:\\新建文件夹\\y.shp";
        File file=new File(path);
        //创建坐标
        String str="{\"type\":\"MultiPolygon\",\"coordinates\":[[[[125.00088033726706,43.40147359017337],[124.99679348328742,43.401096616676305],[124.99694837503591,43.40186442431018],[124.99750584741349,43.401955866321565],[124.99803787708493,43.40230992248794],[124.9980525639301,43.40490599341744],[124.99939124766493,43.4039525077194],[124.999703712491,43.4048699875539],[124.99866141203354,43.40606649155232],[125.00126143620331,43.40562431171862],[125.00257777597052,43.403552077651824],[125.00295876012552,43.40274710316431],[125.00307123554897,43.40186767450569],[125.00088033726706,43.40147359017337]]]]}";
        JSONObject jsonObject = JSONObject.fromObject(str); 
        JSONArray jsonArray = jsonObject.getJSONArray("coordinates"); 
        System.out.println(jsonArray); 
        //创建对象添加属性信息 
        Zdesignpolygon polygon=new Zdesignpolygon();
        polygon.setCode("106");
        polygon.setCoordinate(str);
        polygon.setMark("tktt3");
        polygon.setName("土坎梯田");
        polygon.setState("坡耕地");
        polygon.setNumber(12.64);
        polygon.setSlope(9.0);  
        //添加创建的路径 
        Map<String, Serializable> params = new HashMap<String, Serializable>();

        params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
        ShapefileDataStoreFactory sf = new ShapefileDataStoreFactory();
        //创建shape文件数据仓库根据工厂
        ShapefileDataStore ds = (ShapefileDataStore) sf.createDataStore(file.toURI().toURL());
        ds.setCharset(Charset.forName("GBK"));

        //ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(params);
        //创建要素模板
        SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();

        //设置wgs84格式
        tb.setCRS(DefaultGeographicCRS.WGS84);
        //设置全局状态
        tb.setName("shapefile");    

        //添加属性类型
        tb.add("图斑编码", String.class);
        tb.add("措施代码", String.class);
        tb.add("措施名称", String.class);
        tb.add("利用现状", String.class);
        tb.add("措施数量", Double.class);
        tb.add("坡度", Double.class);
        tb.add("the_geom", MultiPolygon.class);
        //设置FeatureType数据存储。该方法将删除任何现有的本地资源或抛出IOException如果远程数据存储。
        //这个元素附加源和附加Javadoc,因此没有能找到Javadoc。
        ds.createSchema(tb.buildFeatureType());
        //设置编码格式

//         类似的API FeatureReader后,调查组成员功能使用next()你可以叫删除()或修改后写()。
//               如果你不调用这两个方法调用之前hasNext(),或下(),这个功能将被修改的。
//            这个API允许修改,编写和基于过滤器的修改。请参阅ContentDataStore使用这个API实现共同opperations的例子。
    //    FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);
        FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = ds.getFeatureWriter(ds.getTypeNames()[0], null);
//      FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = ds.getFeatureWriter(Transaction.AUTO_COMMIT);
        SimpleFeature feature = featureWriter.next();
        // 取出特征里面的属性集合
        //添加属性值和坐标
        feature.setAttribute(0, polygon.getCode());
        feature.setAttribute(1, polygon.getMark());
        feature.setAttribute(2, polygon.getName());
        feature.setAttribute(3, polygon.getState());
        feature.setAttribute(4, polygon.getNumber());
        feature.setAttribute(5, polygon.getSlope());
        //转换geojson 
        GeometryJSON geometryJSON2 = new GeometryJSON(15);
        Geometry geometry = geometryJSON2.read(new StringReader(polygon.getCoordinate()));
        feature.setAttribute("the_geom", geometry);

        featureWriter.write();
        featureWriter.close();      
        ds.dispose();

    }



    protected boolean validating;
    protected Map<String, String> index;
    protected SimpleFeatureType featureType;
    protected Object[] values;
    public SimpleFeatureType getFeatureType() {
        return this.featureType;
    }


//  public void setAttribute(String name, Object value) {
//      String idx =  this.index.get(name);
//      if (idx == null) {
//          throw new IllegalAttributeException("Unknown attribute " + name);
//      } else {
//          this.setAttribute(idx, value);
//      }
//  }


}

  • 写回答

1条回答 默认 最新

  • jeffszh 2021-01-18 14:26
    关注

    同问,没人回答吗?

     

    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错