把经纬度坐标转成shp文件

import java.io.File;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.geotools.data.FeatureWriter;
import org.geotools.data.FileDataStoreFactorySpi;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.shapefile.files.ShpFiles;
import org.geotools.data.shapefile.shp.ShapefileReader;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import com.xxx.config.CommonConfig;
import com.xxx.tool.ConnectionManager;
import com.xxx.tool.LogTool;
import com.xxx.tool.PropertiesUtil;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

public class writeShp {
    
    public static void main(String[] args) throws Exception {
        write("H:\\TEST\\shp1\\test.shp");
    }
    
    public static void queryData(String filepath) {  
        PropertiesUtil.loadSysCofing();
        Connection conn = null;
        try {
            conn = getConn();
            conn.setAutoCommit(false);
            PreparedStatement pstmt = conn.prepareStatement("");
            
        } catch (SQLException ex) {
            // LogTool.logger.error("批量更新出错,错误信息为"+ex.getMessage());
            LogTool.logger.error("更新出错,错误信息为" + ex.getMessage());
            try {
                conn.rollback();
            } catch (Exception e) {
                e.printStackTrace();
            }

        } finally {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    
    }
    public static void write(String filepath) {  
        try {  
            //创建shape文件对象  
            File file = new File(filepath);  
            Map<String, Serializable> params = new HashMap<String, Serializable>();  
            params.put( ShapefileDataStoreFactory.URLP.key, file.toURI().toURL() );  
            ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);  
            //定义图形信息和属性信息  
            SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();  
            tb.setCRS(DefaultGeographicCRS.WGS84);  
            tb.setName("shapefile");  
            tb.add("the_geom", Point.class);  
            tb.add("POIID", Long.class);  
            tb.add("NAMEC", String.class);  
            ds.createSchema(tb.buildFeatureType());  
            ds.setCharset(Charset.forName("GBK"));  
            //设置Writer  
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);  
            //写下一条  
            //SimpleFeature feature = writer.next();  
            //feature.setAttribute("the_geom", new GeometryFactory().createPoint(new Coordinate(116.123, 39.345)));  
            //feature.setAttribute("POIID", 1234567890l);  
            //feature.setAttribute("NAMEC", "某兴趣点1");  
            //feature = writer.next();  
            //feature.setAttribute("the_geom", new GeometryFactory().createPoint(new Coordinate(116.456, 39.678)));  
            //feature.setAttribute("POIID", 1234567891l);  
            //feature.setAttribute("NAMEC", "某兴趣点2");  
            writer.write();  
            writer.close();  
            ds.dispose();  
              
            //读取刚写完shape文件的图形信息  
            ShpFiles shpFiles = new ShpFiles(filepath);  
            ShapefileReader reader = new ShapefileReader(shpFiles, false, true, new GeometryFactory(), false);  
            try {  
                while (reader.hasNext()) {  
                    System.out.println(reader.nextRecord().shape());      
                }  
            } finally {  
                reader.close();  
            }  
        } catch (Exception e) { }  
    }  
     

    //由源shape文件创建新的shape文件
    //Java代码  收藏代码
    public void transShape(String srcfilepath, String destfilepath) {  
        try {  
            //源shape文件  
            ShapefileDataStore shapeDS = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(new File(srcfilepath).toURI().toURL());  
            //创建目标shape文件对象  
            Map<String, Serializable> params = new HashMap<String, Serializable>();  
            FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();  
            params.put(ShapefileDataStoreFactory.URLP.key, new File(destfilepath).toURI().toURL());  
            ShapefileDataStore ds = (ShapefileDataStore) factory.createNewDataStore(params);  
            // 设置属性  
            SimpleFeatureSource fs = shapeDS.getFeatureSource(shapeDS.getTypeNames()[0]);  
            //下面这行还有其他写法,根据源shape文件的simpleFeatureType可以不用retype,而直接用fs.getSchema设置  
            ds.createSchema(SimpleFeatureTypeBuilder.retype(fs.getSchema(), DefaultGeographicCRS.WGS84));  
              
            //设置writer  
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);  
              
            //写记录  
            SimpleFeatureIterator it = fs.getFeatures().features();  
            try {  
                while (it.hasNext()) {  
                    SimpleFeature f = it.next();  
                    SimpleFeature fNew = writer.next();  
                    fNew.setAttributes(f.getAttributes());  
                    writer.write();  
                }  
            } finally {  
                it.close();  
            }  
            writer.close();  
            ds.dispose();  
            shapeDS.dispose();  
        } catch (Exception e) { e.printStackTrace();    }  
    } 
    protected static Connection getConn() {
        return getConn(CommonConfig.DBConn.DB_SPMD);
    }
    // 获取数据库连接
    protected static Connection getConn(String connName) {
        return ConnectionManager.getInstance().getConnection(connName);
    }
}
<dependency>
	<groupId>org.geotools</groupId>
	<artifactId>gt-shapefile</artifactId>
	<version>${geotools.version}</version>
</dependency>
<dependency>
	<groupId>org.geotools</groupId>
	<artifactId>gt-geojson</artifactId>
	<version>${geotools.version}</version>
</dependency>
<dependency>
	<groupId>org.geotools</groupId>
	<artifactId>gt-csv</artifactId>
	<version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.58</version>
</dependency>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个关于QGIS和Python编程的问题。我会尽力回答。 首先,需要安装QGIS软件,并确保已经安装了Python插件。然后,可以使用以下Python脚本实现批量将txt经纬度文件shp文件: ```python import os from qgis.core import * import qgis.utils # 设置QGIS应用程序路径 QgsApplication.setPrefixPath("C:/Program Files/QGIS 3.10", True) qgs = QgsApplication([], False) qgs.initQgis() # 设置经纬度txt文件路径 input_folder = "C:/input_folder" # 设置输出shp文件路径 output_folder = "C:/output_folder" # 遍历输入文件夹中的所有txt文件 for file_name in os.listdir(input_folder): if file_name.endswith(".txt"): # 加载txt文件 uri = "file:///" + os.path.join(input_folder, file_name) + "?delimiter=," layer = QgsVectorLayer(uri, file_name[:-4], "delimitedtext") # 设置坐标系 crs = QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId) layer.setCrs(crs) # 保存为shp文件 output_file = os.path.join(output_folder, file_name[:-4] + ".shp") QgsVectorFileWriter.writeAsVectorFormat(layer, output_file, "UTF-8", crs, "ESRI Shapefile") print("Converted {} to {}".format(file_name, output_file)) # 退出QGIS应用程序 qgs.exitQgis() ``` 在这个脚本中,首先设置了QGIS应用程序的路径,并初始化了QGIS应用程序。然后,设置了输入文件夹和输出文件夹的路径,并遍历了输入文件夹中的所有txt文件。对于每个txt文件,首先使用QGIS的delimitedtext数据提供程序加载文件,并设置其坐标系为WGS 84。然后,使用QGIS的ESRI Shapefile格式将图层保存为shp文件。最后,退出QGIS应用程序。 希望这个脚本能够帮助到你。如果你有任何问题,请随时问我。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值