国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

坐標轉(zhuǎn)換-使用geotools讀取和轉(zhuǎn)換地理空間表的坐標系(sqlserver、postgresql)

這篇具有很好參考價值的文章主要介紹了坐標轉(zhuǎn)換-使用geotools讀取和轉(zhuǎn)換地理空間表的坐標系(sqlserver、postgresql)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

前言:

業(yè)務(wù)上通過GIS軟件將空間數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫時,因為不同的數(shù)據(jù)來源和軟件設(shè)置,可能導(dǎo)入到數(shù)據(jù)庫的空間表坐標系是各種各樣的。
如果要把數(shù)據(jù)庫空間表發(fā)布到geoserver并且統(tǒng)一坐標系,只是在geoserver單純的設(shè)置坐標系只是改了定義并沒有實際執(zhí)行坐標轉(zhuǎn)換,所以需要在數(shù)據(jù)庫層面統(tǒng)一好坐標系,再發(fā)布到geoserver。
坐標轉(zhuǎn)換-使用geotools讀取和轉(zhuǎn)換地理空間表的坐標系(sqlserver、postgresql),GIS,sqlserver,postgresql,坐標轉(zhuǎn)換,geotools,geoserver

1,開發(fā)前準備

1.1,數(shù)據(jù)準備

要準備測試數(shù)據(jù),可以參考 地理空間表的導(dǎo)入。
我這里使用arcgis pro導(dǎo)入sqlserver,如果導(dǎo)入postgresql需要企業(yè)數(shù)據(jù)庫才行,也就是需要離線證書,比較麻煩。
我先導(dǎo)入一個4524的投影坐標,測試轉(zhuǎn)換為4490
坐標轉(zhuǎn)換-使用geotools讀取和轉(zhuǎn)換地理空間表的坐標系(sqlserver、postgresql),GIS,sqlserver,postgresql,坐標轉(zhuǎn)換,geotools,geoserver

1.2,環(huán)境準備

坐標轉(zhuǎn)換需要先讀取數(shù)據(jù)庫的空間表原坐標系,在根據(jù)原坐標系轉(zhuǎn)換為目標坐標系。
使用的轉(zhuǎn)換工具是geotool。
pom引入必要的依賴,geotools版本是24.3

<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-main</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
     <groupId>org.geotools</groupId>
     <artifactId>gt-jdbc</artifactId>
     <version>${geotools.version}</version>
 </dependency>
 <dependency>
     <groupId>org.geotools.jdbc</groupId>
     <artifactId>gt-jdbc-sqlserver</artifactId>
     <version>${geotools.version}</version>
 </dependency>
 <dependency>
     <groupId>org.geotools.jdbc</groupId>
     <artifactId>gt-jdbc-postgis</artifactId>
     <version>${geotools.version}</version>
 </dependency>

2,讀取空間表原坐標系

要使用geotool讀取空間表的坐標系,需要先使用geotool提供的方法創(chuàng)建DataStore,官網(wǎng)有一個示例代碼
https://docs.geotools.org/latest/userguide/library/jdbc/sqlserver.html

java.util.Map params = new java.util.HashMap();
params.put( "dbtype", "sqlserver");   //(巨坑)
params.put( "host", "localhost");
params.put( "port", 4866);
params.put( "user", "geotools");
params.put( "passwd", "geotools");
DataStore dataStore=DataStoreFinder.getDataStore(params);

這是一個坑,官方說明是版本14之后支持Microsoft JDBC driver,dbtype應(yīng)該就不需要使用jtds前綴了,實際上不加必報錯

先寫一個測試方法,傳入數(shù)據(jù)庫連接信息,表名,數(shù)據(jù)庫類型,返回原表坐標系

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.jdbc.JDBCDataStoreFactory;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

public static int getEpsg(DatabaseConfig databaseConfig, String tableName) {
        DataStore dataStore = null;
        try {
            Map<String, Object> params = new HashMap<>();
//        params.put(JDBCDataStoreFactory.SCHEMA.key, "dbo");
           if (DatabaseType.SQLSERVER.equals(databaseConfig.getDatabaseType())) {
                params.put(JDBCDataStoreFactory.DBTYPE.key, "jtds-sqlserver");
            } else {
                params.put(JDBCDataStoreFactory.DBTYPE.key, "jtds-postgis");
            }
            params.put(JDBCDataStoreFactory.HOST.key, databaseConfig.getHost());
            params.put(JDBCDataStoreFactory.PORT.key, databaseConfig.getPort());
            params.put(JDBCDataStoreFactory.DATABASE.key, databaseConfig.getDatabaseName());
            params.put(JDBCDataStoreFactory.USER.key, databaseConfig.getUsername());
            params.put(JDBCDataStoreFactory.PASSWD.key, databaseConfig.getPassword());
            dataStore = DataStoreFinder.getDataStore(params);
            if (dataStore == null) {
                System.out.println("Failed to connect to the database.");
                return -1;
            }
            // Get the feature source for the "aa" table
            SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName);
            // Get the feature type and its CRS
            SimpleFeatureType featureType = featureSource.getSchema();
            CoordinateReferenceSystem crs = featureType.getCoordinateReferenceSystem();
            // Print the CRS details
            if (crs != null) {
                System.out.println("Spatial Reference System: " + crs.getName());
                System.out.println("EPSG Code: " + crs.getName().getCode());
                System.out.println("crs : " + crs.toString());
                //抽取原表坐標系
                int result = extractEPSG(crs.toString());
                System.out.println("Result: " + result);
                return result;
            }
            // Close the data store
            dataStore.dispose();
            return 0;
        } catch (IOException e) {
            log.error("查詢空間表坐標系異常:{}", e.toString());
            return -1;
        } finally {
            if (dataStore != null) {
                dataStore.dispose();
            }
        }
    }

然后看一下解析出來坐標信息

Spatial Reference System: EPSG:CGCS2000 / 3-degree Gauss-Kruger zone 36
EPSG Code: CGCS2000 / 3-degree Gauss-Kruger zone 36
crs : PROJCS["CGCS2000 / 3-degree Gauss-Kruger zone 36", 
  GEOGCS["China Geodetic Coordinate System 2000", 
    DATUM["China 2000", 
      SPHEROID["CGCS2000", 6378137.0, 298.257222101, AUTHORITY["EPSG","1024"]], 
      AUTHORITY["EPSG","1043"]], 
    PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], 
    UNIT["degree", 0.017453292519943295], 
    AXIS["Geodetic latitude", NORTH], 
    AXIS["Geodetic longitude", EAST], 
    AUTHORITY["EPSG","4490"]], 
  PROJECTION["Transverse_Mercator", AUTHORITY["EPSG","9807"]], 
  PARAMETER["central_meridian", 108.0], 
  PARAMETER["latitude_of_origin", 0.0], 
  PARAMETER["scale_factor", 1.0], 
  PARAMETER["false_easting", 36500000.0], 
  PARAMETER["false_northing", 0.0], 
  UNIT["m", 1.0], 
  AXIS["Northing", NORTH], 
  AXIS["Easting", EAST], 
  AUTHORITY["EPSG","4524"]]

我想要的是之前我們在arcgis pro中看到的投影坐標,位于crs信息的最后一個EPSG內(nèi),針對crs信息寫一個方法解析出epsg

    public static int extractEPSG(String input) {
        Pattern pattern = Pattern.compile("AUTHORITY\\[\"EPSG\",\"(\\d+)\"\\]");
        Matcher matcher = pattern.matcher(input);

        int lastEPSG = 0;
        while (matcher.find()) {
            lastEPSG = Integer.parseInt(matcher.group(1));
        }
        return lastEPSG;
    }

3,執(zhí)行坐標轉(zhuǎn)換

我這里目標坐標系寫死,因為系統(tǒng)需要插入到sqlserver中的都要統(tǒng)一坐標系,所以直接在原表更新了。
如果要保留原表信息可以復(fù)制表在副本表更新坐標。
sqlserver與postgresql中空間函數(shù)有些差異,需要區(qū)分處理。

/**
     * 地理空間表坐標轉(zhuǎn)換
     *
     * @param sourceEpsg     原表坐標系
     * @param config         數(shù)據(jù)庫連接信息
     * @param tableName      表名  dbo.ROAD
     * @param geometryColumn 空間字段
     */
    public static void epsgTo4490(int sourceEpsg, DatabaseConfig config, String tableName, String geometryColumn) {
        String sourceEPSG = "EPSG:" + sourceEpsg;
        String targetEPSG = "EPSG:4490";
        ResultSet resultSet = null;
        try (Connection connection = DatabaseConnection.getConnection(config)) {
            //拼接sql
            String sql;
            if (config.getDatabaseType().SQLSERVER.equals(config.getDatabaseType())) {
                sql = "SELECT " + geometryColumn + ".STAsText() as Shape,OBJECTID FROM " + tableName;
            } else {
                //ST_AsText(columns)
                sql = "SELECT ST_AsText(" + geometryColumn + ") as Shape,OBJECTID FROM " + tableName;
            }

            // 使用連接執(zhí)行 SQL 查詢操作
            PreparedStatement statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();

            // Create MathTransform
            CRSFactory crsFactory = new CRSFactory();
            org.osgeo.proj4j.CoordinateReferenceSystem sourceCRS = crsFactory.createFromName(sourceEPSG);
            org.osgeo.proj4j.CoordinateReferenceSystem targetCRS = crsFactory.createFromName(targetEPSG);
            CoordinateTransformFactory transformFactory = new CoordinateTransformFactory();
            CoordinateTransform transform = transformFactory.createTransform(sourceCRS, targetCRS);

            // Process each row of the result set
            while (resultSet.next()) {
                String shape = resultSet.getString("Shape");
                int objectId = resultSet.getInt("OBJECTID");

                // Convert the string representation of the geometry to a JTS Geometry object
                WKTReader reader = new WKTReader();
                Geometry geometry = reader.read(shape);

                // Perform the coordinate transformation for each coordinate in the geometry
                for (int i = 0; i < geometry.getCoordinates().length; i++) {
                    Coordinate srcCoord = geometry.getCoordinates()[i];
                    ProjCoordinate targetCoord = new ProjCoordinate(srcCoord.getX(), srcCoord.getY());
                    transform.transform(targetCoord, targetCoord); // 將源坐標轉(zhuǎn)換為目標坐標,并保存在 targetCoord 中
                    srcCoord.setX(targetCoord.x);
                    srcCoord.setY(targetCoord.y);
                }

                // Convert the transformed geometry back to a string
                WKTWriter writer = new WKTWriter();
                String transformedShape = writer.write(geometry);

                // Update the original table with the transformed geometry using the primary key
                String updateSQL;
                if (DatabaseType.SQLSERVER.equals(config.getDatabaseType())) {
                    updateSQL = "UPDATE " + tableName + " SET " + geometryColumn + " = ? WHERE OBJECTID = ?";
                } else {
                    //UPDATE "public"."ROAD" SET Shape = ST_SetSRID(ST_GeomFromText("Shape"), 4490);
                    updateSQL = "UPDATE " + tableName + " SET " + geometryColumn + " = ST_SetSRID(?,4490) WHERE OBJECTID = ?";
                }
                statement = connection.prepareStatement(updateSQL);
                statement.setString(1, transformedShape);
                statement.setInt(2, objectId);
                statement.executeUpdate();
                statement.clearParameters();
            }
            if (DatabaseType.SQLSERVER.equals(config.getDatabaseType())) {
                //修復(fù)多邊形錯誤   UPDATE dbo.ROAD SET Shape = Shape.MakeValid()
                String updateSQL = "UPDATE " + tableName + " SET " + geometryColumn + " = " + geometryColumn + ".MakeValid()";
                statement = connection.prepareStatement(updateSQL);
                statement.executeUpdate();
                //指定坐標系 UPDATE dbo.ROAD SET Shape.STSrid=4490
                updateSQL = "UPDATE " + tableName + " SET " + geometryColumn + ".STSrid=4490";
                statement = connection.prepareStatement(updateSQL);
                statement.executeUpdate();
            }
            // Close the resources
            statement.close();
            resultSet.close();
        } catch (SQLException e) {
            log.error("坐標轉(zhuǎn)換中sql執(zhí)行異常:{}", e.getMessage());
        } catch (ParseException e) {
            log.error("坐標轉(zhuǎn)換中異常:{}", e.getMessage());
        }
    }

上述代碼只是sqlservcer親測多種坐標系轉(zhuǎn)換正常,且轉(zhuǎn)換后的表發(fā)布到geoserver和arcgis都能正常預(yù)覽且聚焦位置正確,postgresql還有待測試文章來源地址http://www.zghlxwxcb.cn/news/detail-631339.html

4,單元測試

    public static void main(String[] args) throws SQLException {
        String tableName = "ROAD";
        //測試sqlserver
        DatabaseConfig databaseConfig = new DatabaseConfig(DatabaseType.SQLSERVER, "127.0.0.1", 1433, "測試中文數(shù)據(jù)庫", "sa", "xxxx");
        //測試postgresql
        //DatabaseConfig databaseConfig = new DatabaseConfig(DatabaseType.POSTGRESQL, "127.0.0.1", 5432, "postgis20", "postgres", "xxxxxxx");
        int sourceEpsg = TableEpsgUtil.getEpsg(databaseConfig, tableName);
        System.out.println("原表坐標:" + sourceEpsg);
        //如果獲取到原表坐標并且不是4490,則執(zhí)行轉(zhuǎn)換
        if (sourceEpsg > 0 && sourceEpsg != 4490) {
            epsgTo4490(sourceEpsg, databaseConfig, tableName, "Shape");
            System.out.println("坐標轉(zhuǎn)換完成");
        }
    }

到了這里,關(guān)于坐標轉(zhuǎn)換-使用geotools讀取和轉(zhuǎn)換地理空間表的坐標系(sqlserver、postgresql)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • vue中使用coordtransform 互相轉(zhuǎn)換坐標系

    vue中使用coordtransform 互相轉(zhuǎn)換坐標系

    官方網(wǎng)站:https://www.npmjs.com/package/coordtransform ?在使用高德sdk時,其返回的坐標在地圖上顯示時有幾百米的偏移,這是由于高德用的是?火星坐標(GCJ02),而不是wgs84坐標。為了消除偏移,將GCJ02轉(zhuǎn)為wgs84即可,可通coordtransform庫來轉(zhuǎn)換。? 安裝完成后可以看到 根據(jù)自己的需要

    2024年02月07日
    瀏覽(23)
  • 機器人坐標系轉(zhuǎn)換從局部坐標系轉(zhuǎn)換到世界坐標系

    機器人坐標系轉(zhuǎn)換從局部坐標系轉(zhuǎn)換到世界坐標系

    矩陣方式: 下面是代碼: 函數(shù)方式: 根據(jù)三角函數(shù)的特性,可以進行一下簡化: 下面是簡化前的代碼示例:

    2024年04月16日
    瀏覽(24)
  • 相機坐標系、像素坐標系轉(zhuǎn)換

    相機內(nèi)參矩陣是相機的重要參數(shù)之一,它描述了相機光學(xué)系統(tǒng)的內(nèi)部性質(zhì),例如焦距、光學(xué)中心和圖像畸變等信息。在計算機視覺和圖形學(xué)中,相機內(nèi)參矩陣通常用于將圖像坐標系中的像素坐標轉(zhuǎn)換為相機坐標系中的三維坐標,或者將相機坐標系中的三維坐標投影到圖像坐標

    2024年02月13日
    瀏覽(25)
  • 坐標轉(zhuǎn)換(相機坐標系、世界坐標系、圖像物理坐標系、圖像像素坐標系)

    坐標轉(zhuǎn)換(相機坐標系、世界坐標系、圖像物理坐標系、圖像像素坐標系)

    一般情況下我們所涉及到的坐標包括四個,即相機坐標系、世界坐標系、圖像物理坐標系、圖像像素坐標系。我們本文的講解思路是在講解每個坐標轉(zhuǎn)換之前先講清楚每個坐標系所表示的含義。本文主要參考由高翔主編的視覺SLAM十四講第五章相機模型。 相機將三維世界的坐

    2024年02月09日
    瀏覽(23)
  • 世界坐標系、相機坐標系和圖像坐標系的轉(zhuǎn)換

    世界坐標系、相機坐標系和圖像坐標系的轉(zhuǎn)換

    之前只是停留在會用的階段,一直沒去讀懂計算的原理,今天通讀了大佬的文章,寫的言簡意賅,感謝感謝~~特此記錄一下,僅用作個人筆記 貼鏈接,十分感謝~ https://blog.csdn.net/weixin_44278406/article/details/112986651 https://blog.csdn.net/guyuealian/article/details/104184551 將三維物體轉(zhuǎn)換成照

    2023年04月15日
    瀏覽(35)
  • Cesium:CGCS2000坐標系的xyz坐標轉(zhuǎn)換成WGS84坐標系的經(jīng)緯高度,再轉(zhuǎn)換到笛卡爾坐標系的xyz坐標

    Cesium:CGCS2000坐標系的xyz坐標轉(zhuǎn)換成WGS84坐標系的經(jīng)緯高度,再轉(zhuǎn)換到笛卡爾坐標系的xyz坐標

    作者:CSDN @ _樂多_ 本文將介紹使用 Vue 、cesium、proj4 框架,實現(xiàn)將CGCS2000坐標系的xyz坐標轉(zhuǎn)換成WGS84坐標系的經(jīng)緯高度,再將WGS84坐標系的經(jīng)緯高度轉(zhuǎn)換到笛卡爾坐標系的xyz坐標的代碼。并將輸入和輸出使用 Vue 前端框架展示了出來。代碼即插即用。 網(wǎng)頁效果如下圖所示, 一、

    2024年02月06日
    瀏覽(51)
  • Unity坐標系的轉(zhuǎn)換—世界坐標轉(zhuǎn)為UI坐標

    直接調(diào)用WorldToAnchorPos,傳入對應(yīng)的參數(shù)返回UGUI坐標

    2024年04月13日
    瀏覽(24)
  • 柱坐標系與直角坐標系的轉(zhuǎn)換

    柱坐標系與直角坐標系的轉(zhuǎn)換

    1.柱坐標系轉(zhuǎn)化為直角坐標系:柱坐標系(r,φ,z)與直角坐標系(x,y,z)的轉(zhuǎn)換關(guān)系 x=rcosφ y=rsinφ z=z 2.直角坐標系轉(zhuǎn)化為柱坐標系:直角坐標系(x,y,z)與柱坐標系(r,φ,z)的轉(zhuǎn)換關(guān)系: r= φ= z=z

    2024年02月11日
    瀏覽(47)
  • 圖像坐標系如何轉(zhuǎn)換到相機坐標系。

    圖像坐標系如何轉(zhuǎn)換到相機坐標系。

    問題描述:圖像坐標系如何轉(zhuǎn)換到相機坐標系。 問題解答: 圖像坐標系的定義: 圖像坐標系是用于描述數(shù)字圖像中像素位置的坐標系。圖像坐標系的原點是相機光軸與成像平面的交點。X軸沿著成像平面的水平方向正向,Y軸沿著成像平面的垂直方向正向。 相機坐標系的定義

    2024年02月04日
    瀏覽(21)
  • 【學(xué)習筆記】空間坐標系旋轉(zhuǎn)與四元數(shù)

    【學(xué)習筆記】空間坐標系旋轉(zhuǎn)與四元數(shù)

    ??最近在學(xué)慣性器件,想著先把理論知識脈絡(luò)打通,于是便開始學(xué)習空間坐標系旋轉(zhuǎn)和四元數(shù),正好結(jié)合剛剛結(jié)課的課程《機器人控制技術(shù)》,記錄一下學(xué)習心得。 旋轉(zhuǎn)矩陣和齊次變換矩陣部分主要參考自教材 《機器人學(xué)導(dǎo)論》 中的第2章?【有需要的可以去z-library上免費

    2024年02月01日
    瀏覽(19)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包