Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
X
xplot_server
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
digit_plot
xplot_server
Commits
d5cbaec9
Commit
d5cbaec9
authored
Nov 09, 2024
by
dliangx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
圈地数据导入
parent
503ff447
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
206 additions
and
3 deletions
+206
-3
data/testdata/data.json
+0
-0
data/trans.go
+118
-3
data/trans_test.go
+88
-0
No files found.
data/testdata/data.json
0 → 100644
View file @
d5cbaec9
This diff is collapsed.
Click to expand it.
data/trans.go
View file @
d5cbaec9
package
data
import
(
"database/sql"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"time"
"com.dliangx.xplot/appserver/model"
geojson
"github.com/paulmach/go.geojson"
)
type
Msdata
struct
{
...
...
@@ -23,6 +29,8 @@ type Msdata struct {
}
func
(
data
Msdata
)
TransToOptArea
()
(
area
model
.
OptArea
)
{
// 设置基本属性
area
.
Properties
=
make
(
map
[
string
]
interface
{})
area
.
Properties
[
"id"
]
=
data
.
ID
area
.
Properties
[
"member_land_id"
]
=
data
.
MemberLandId
area
.
Properties
[
"block_crop"
]
=
data
.
BlockCrop
...
...
@@ -33,14 +41,121 @@ func (data Msdata) TransToOptArea() (area model.OptArea) {
area
.
Properties
[
"preview_image_url"
]
=
data
.
PreviewImageUrl
area
.
Properties
[
"block_id"
]
=
data
.
BlockId
area
.
Properties
[
"create_time"
]
=
data
.
CreateTime
// 设置类型
area
.
Type
=
"polygon"
// 解析更新时间
ptime
,
err
:=
time
.
Parse
(
"2006-01-02 15:04:05"
,
data
.
UpdateTime
)
if
err
!=
nil
{
fmt
.
Println
(
"时间格式错误
.."
)
fmt
.
Println
(
"时间格式错误
:"
,
err
)
}
area
.
OptTime
=
ptime
// data.EnclosureCoordinates
fmt
.
Printf
(
"area: %v
\n
"
,
area
)
// 解析围栏坐标为geometry
geometry
,
err
:=
data
.
parseEnclosureCoordinates
()
if
err
!=
nil
{
fmt
.
Println
(
"解析围栏坐标错误:"
,
err
)
area
.
Geometry
=
nil
}
else
{
area
.
Geometry
=
geometry
}
return
area
}
func
(
data
Msdata
)
parseEnclosureCoordinates
()
(
*
geojson
.
Geometry
,
error
)
{
// 移除空格
coordStr
:=
strings
.
TrimSpace
(
data
.
EnclosureCoordinates
)
// 创建geometry结构
geometry
:=
&
geojson
.
Geometry
{
Type
:
"Polygon"
,
}
// 解析坐标字符串
var
coordinates
[][][]
float64
if
err
:=
json
.
Unmarshal
([]
byte
(
coordStr
),
&
coordinates
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"解��坐标失败: %v"
,
err
)
}
// 设置坐标
geometry
.
Polygon
=
coordinates
return
geometry
,
nil
}
// ProcessJsonData 处理JSON数据并写入数据库
func
ProcessJsonData
(
jsonData
[]
byte
,
db
*
sql
.
DB
)
error
{
// 开始事务
tx
,
err
:=
db
.
Begin
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"开始事务失败: %v"
,
err
)
}
defer
tx
.
Rollback
()
// 解析JSON数据
var
msdataList
[]
Msdata
if
err
:=
json
.
Unmarshal
(
jsonData
,
&
msdataList
);
err
!=
nil
{
return
fmt
.
Errorf
(
"解析JSON失败: %v"
,
err
)
}
// 处理每条记录
for
_
,
msdata
:=
range
msdataList
{
optArea
:=
msdata
.
TransToOptArea
()
// 将 Properties 和 Geometry 转换为 JSON
propertiesJSON
,
err
:=
json
.
Marshal
(
optArea
.
Properties
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Properties转JSON失败: %v"
,
err
)
}
geometryJSON
,
err
:=
json
.
Marshal
(
optArea
.
Geometry
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Geometry转JSON失败: %v"
,
err
)
}
// 写入数据库
_
,
err
=
tx
.
Exec
(
`
INSERT INTO opt_areas (
type,
properties,
geometry,
opt_time
) VALUES ($1, $2, $3, $4)
`
,
optArea
.
Type
,
propertiesJSON
,
// 使用JSON格式
geometryJSON
,
// 使用JSON格式
optArea
.
OptTime
,
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"插入数据失败: %v"
,
err
)
}
}
// 提交事务
if
err
:=
tx
.
Commit
();
err
!=
nil
{
return
fmt
.
Errorf
(
"提交事务失败: %v"
,
err
)
}
return
nil
}
// ProcessJsonFile 从文件读取并处理JSON数据
func
ProcessJsonFile
(
filePath
string
,
db
*
sql
.
DB
)
error
{
// 读取JSON文件
jsonData
,
err
:=
os
.
ReadFile
(
filePath
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"读取JSON文件失败: %v"
,
err
)
}
// 使用已有的ProcessJsonData处理数据
if
err
:=
ProcessJsonData
(
jsonData
,
db
);
err
!=
nil
{
return
fmt
.
Errorf
(
"处理数据失败: %v"
,
err
)
}
log
.
Printf
(
"成功处理文件: %s"
,
filePath
)
return
nil
}
data/trans_test.go
0 → 100644
View file @
d5cbaec9
package
data
import
(
"database/sql"
"os"
"path/filepath"
"testing"
_
"github.com/lib/pq"
)
func
TestProcessJsonFile
(
t
*
testing
.
T
)
{
// 设置测试数据库连接
dbURL
:=
os
.
Getenv
(
"TEST_DATABASE_URL"
)
if
dbURL
==
""
{
dbURL
=
"postgres://liang:postgres@localhost/xpolt?sslmode=disable"
}
// 连接数据库
db
,
err
:=
sql
.
Open
(
"postgres"
,
dbURL
)
if
err
!=
nil
{
t
.
Fatalf
(
"数据库连接失败: %v"
,
err
)
}
defer
db
.
Close
()
// 测试数据库连接
if
err
:=
db
.
Ping
();
err
!=
nil
{
t
.
Fatalf
(
"数据库连接测试失败: %v"
,
err
)
}
// 获取测试文件路径
testFile
:=
filepath
.
Join
(
"testdata"
,
"data.json"
)
// 测试文件处理
err
=
ProcessJsonFile
(
testFile
,
db
)
if
err
!=
nil
{
t
.
Errorf
(
"处理JSON文件失败: %v"
,
err
)
}
}
func
TestMsdataTransToOptArea
(
t
*
testing
.
T
)
{
// 创建测试数据
testData
:=
Msdata
{
ID
:
1
,
MemberLandId
:
4
,
BlockCrop
:
"玉米"
,
BlockArea
:
100.0000
,
BlockCoordinates
:
"112.815624,37.460469"
,
EnclosureCoordinates
:
`[[[112.814746,37.459434],[112.813547,37.460806],
[112.814086,37.462012],[112.815706,37.461738],[112.816799,37.461269],
[112.817074,37.460346],[112.816888,37.45944],[112.816145,37.458709]]]`
,
CreateTime
:
"2024-04-02 11:19:44"
,
UpdateTime
:
"2024-04-10 18:06:51"
,
}
// 测试转换
optArea
:=
testData
.
TransToOptArea
()
// 验证转换结果
if
optArea
.
Type
!=
"polygon"
{
t
.
Errorf
(
"期望类型为 polygon, 得到 %s"
,
optArea
.
Type
)
}
if
optArea
.
Properties
[
"id"
]
!=
testData
.
ID
{
t
.
Errorf
(
"期望 ID 为 %d, 得到 %v"
,
testData
.
ID
,
optArea
.
Properties
[
"id"
])
}
if
optArea
.
Geometry
==
nil
{
t
.
Error
(
"Geometry 不应该为 nil"
)
}
}
func
TestParseEnclosureCoordinates
(
t
*
testing
.
T
)
{
testData
:=
Msdata
{
EnclosureCoordinates
:
`[[[112.814746,37.459434],[112.813547,37.460806],
[112.814086,37.462012],[112.815706,37.461738]]]`
,
}
geometry
,
err
:=
testData
.
parseEnclosureCoordinates
()
if
err
!=
nil
{
t
.
Errorf
(
"解析坐标失败: %v"
,
err
)
}
if
geometry
.
Type
!=
"Polygon"
{
t
.
Errorf
(
"期望类型为 Polygon, 得到 %s"
,
geometry
.
Type
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment