欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

目錄

java實現(xiàn)將shp文件倒入到postgresql

使用GeoTools將shp文件倒入到postgresql,首先需要將geotools下的gt-shapefile包下載下來,但是這個包下載的時候一般不能直接下載下來。

pom文件中加入的依賴內(nèi)容是

        <dependency>
			<groupId>org.geotools</groupId>
			<artifactId>gt-shapefile</artifactId>
			<version>24.0</version>
		</dependency>
		<dependency>
			<groupId>org.geotools.jdbc</groupId>
			<artifactId>gt-jdbc-postgis</artifactId>
			<version>24.0</version>
		</dependency>

gt-jdbc-postgis這個可以直接下載

解決方式:

1:從maven倉庫直接下來然后找到本地倉庫的org.geotools.gt-shapefile.版本號,然后將jar包放進去,更新項目的pom文件即可

maven倉庫的地址:https://mvnrepository.com/artifact/org.geotools

2:我這里已經(jīng)下載了24.0的版本

百度網(wǎng)盤下載

以上內(nèi)容解決之后還需要將repository的內(nèi)容的加上

        <repository>
			<id>osgeo</id>
			<name>OSGeo Release Repository</name>
			<url>https://repo.osgeo.org/repository/release/</url>
			<snapshots><enabled>false</enabled></snapshots>
			<releases><enabled>true</enabled></releases>
		</repository>
		<repository>
			<id>osgeo-snapshot</id>
			<name>OSGeo Snapshot Repository</name>
			<url>https://repo.osgeo.org/repository/snapshot/</url>
			<snapshots><enabled>true</enabled></snapshots>
			<releases><enabled>false</enabled></releases>
		</repository>

加完之后,需要等待pom文件更新,完成之后準備工作已經(jīng)完成

以下是實現(xiàn)倒入的代碼

public static final String pg_dbtype = "postgis";

	public static final String pg_host = "127.0.0.1"; 

	public static final Integer pg_port = 5432;

	public static final String pg_schema = "public";

	public static final String pg_database = "test";

	public static final String pg_user = "postgres";

	public static final String pg_password = "123456";

	public static void main(String[] args) {
		File file = new File("自己的shp文件的地址");
		importShpFileMethod(file);
	}

	/**
	 * @param file shp文件的位置
	 * @return
	 * @author 
	 * @description 倒入shp文件
	 */
	public static void importShpFileMethod(File file) {
		JDBCDataStore jdbcDataStore = connectPostgis();
		SimpleFeatureSource simpleFeatureSource = readSHP(file);
		JDBCDataStore ds = createTable(jdbcDataStore, simpleFeatureSource);
		writeShp2Postgis(ds, simpleFeatureSource);
	}

	/**
	 * 鏈接到postgis
	 *
	 * @return
	 */
	private static JDBCDataStore connectPostgis() {
		JDBCDataStore jdbcDataStore = null;
		DataStore dataStore;
		Map<String, Object> params = new HashMap<>(16);
		params.put("dbtype", pg_dbtype);
		params.put("host", pg_host);
		params.put("port", pg_port);
		params.put("schema", pg_schema);
		params.put("database", pg_database);
		params.put("user", pg_user);
		params.put("passwd", pg_password);

		try {
			dataStore = DataStoreFinder.getDataStore(params);
			if (dataStore != null) {
				jdbcDataStore = (JDBCDataStore) dataStore;
				System.out.println("鏈接數(shù)據(jù)庫成功");
			} else {
				System.out.println("鏈接數(shù)據(jù)庫失敗");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jdbcDataStore;
	}

	/**
	 * 讀取shp文件
	 *
	 * @param file
	 * @return
	 */
	private static SimpleFeatureSource readSHP(File file) {
		//數(shù)據(jù)源
		SimpleFeatureSource simpleFeatureSource = null;
		try {
			//定義數(shù)據(jù)存儲
			ShapefileDataStore shapefileDataStore = new ShapefileDataStore(file.toURI().toURL());
			//設置編碼
			Charset charset = shapefileDataStore.getCharset();
			shapefileDataStore.setCharset(charset);
			String tableName = shapefileDataStore.getTypeNames()[0];
			//獲取數(shù)據(jù)源
			simpleFeatureSource = shapefileDataStore.getFeatureSource(tableName);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return simpleFeatureSource;
	}


	/**
	 * 創(chuàng)建表
	 */
	private static JDBCDataStore createTable(JDBCDataStore jdbcDataStore, SimpleFeatureSource simpleFeatureSource) {
		SimpleFeatureType simpleFeatureType = simpleFeatureSource.getSchema();
		try {
			jdbcDataStore.createSchema(simpleFeatureType);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jdbcDataStore;
	}

	/**
	 * 把shp寫入postgis
	 *
	 * @param jdbcDataStore
	 * @param simpleFeatureSource
	 */
	private static void writeShp2Postgis(JDBCDataStore jdbcDataStore, SimpleFeatureSource simpleFeatureSource) {
		//獲取模式
		SimpleFeatureType simpleFeatureType = simpleFeatureSource.getSchema();
		FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;
		//開始寫入數(shù)據(jù)
		try {
			//獲取要素寫入
			writer = jdbcDataStore.getFeatureWriter(simpleFeatureType.getTypeName().toLowerCase(), Transaction.AUTO_COMMIT);
			//獲取數(shù)據(jù)源
			SimpleFeatureCollection simpleFeatureCollection = simpleFeatureSource.getFeatures();
			//獲取要素遍歷器
			SimpleFeatureIterator iterator = simpleFeatureCollection.features();
			while (iterator.hasNext()) {
				//寫入下一個
				writer.hasNext();
				//要寫入的空要素
				SimpleFeature writeNext = writer.next();
				//遍歷獲得的要素
				SimpleFeature simpleFeature = iterator.next();
				//賦值屬性
				for (int i = 0; i < simpleFeature.getAttributeCount(); i++) {
					writeNext.setAttribute(i, simpleFeature.getAttribute(i));
				}
				//寫入
				writer.write();
			}
			System.out.println("導入成功");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (writer != null) {
					writer.close();
				}
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
			if (jdbcDataStore != null) {
				jdbcDataStore.dispose();
			}
		}
	}

按照自己的需求配置數(shù)據(jù)庫和文件的位置即可完成


本文內(nèi)容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。

轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://m.gantiao.com.cn/post/18919629.html

評論列表
雨后初晴的彩虹橋

代碼中沒有處理異常和錯誤,例如數(shù)據(jù)庫連接失敗、文件讀取失敗等。

2025-06-06 11:12:41回復

您暫未設置收款碼

請在主題配置——文章設置里上傳

掃描二維碼手機訪問

文章目錄