Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
邬吉朋
pipeline-network
Commits
b74bcf3f
Commit
b74bcf3f
authored
Jul 20, 2026
by
wujipeng
Browse files
init
parents
Pipeline
#340
failed with stages
in 0 seconds
Changes
24
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/com/example/pipelinenetwork/service/impl/RoadServiceImpl.java
0 → 100644
View file @
b74bcf3f
package
com.example.pipelinenetwork.service.impl
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.example.pipelinenetwork.mapper.RoadMapper
;
import
com.example.pipelinenetwork.pojo.entity.RoadEntity
;
import
com.example.pipelinenetwork.pojo.vo.RoadVO
;
import
com.example.pipelinenetwork.service.RoadService
;
import
com.example.pipelinenetwork.util.LinestringUtil
;
import
org.apache.poi.ss.usermodel.Row
;
import
org.apache.poi.ss.usermodel.Sheet
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
org.apache.poi.ss.usermodel.WorkbookFactory
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.InputStream
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
@Service
public
class
RoadServiceImpl
extends
ServiceImpl
<
RoadMapper
,
RoadEntity
>
implements
RoadService
{
@Override
public
RoadVO
match
(
Double
lng
,
Double
lat
)
{
RoadVO
roadVO
=
new
RoadVO
();
Map
<
String
,
Object
>
result
=
baseMapper
.
matchNearestRoad
(
lng
,
lat
);
if
(
result
==
null
||
result
.
isEmpty
())
{
roadVO
.
setFound
(
false
);
}
else
{
roadVO
.
setFound
(
true
);
roadVO
.
setName
(
result
.
get
(
"name"
).
toString
());
roadVO
.
setDistance
(
Double
.
parseDouble
(
result
.
get
(
"distance"
).
toString
()));
roadVO
.
setId
(
Long
.
parseLong
(
result
.
get
(
"id"
).
toString
()));
String
geomWkt
=
result
.
get
(
"geomWkt"
).
toString
();
List
<
List
<
Double
>>
lists
=
LinestringUtil
.
parseLinestring
(
geomWkt
);
roadVO
.
setCoordinates
(
lists
);
}
return
roadVO
;
}
@Override
public
List
<
String
>
importRoadData
(
MultipartFile
file
)
throws
Exception
{
return
parseAndGenerateSQL
(
file
);
}
/**
* 解析Excel并生成SQL语句(不执行)
*/
public
List
<
String
>
parseAndGenerateSQL
(
MultipartFile
file
)
throws
Exception
{
List
<
String
>
sqlStatements
=
new
ArrayList
<>();
try
(
InputStream
inputStream
=
file
.
getInputStream
();
Workbook
workbook
=
WorkbookFactory
.
create
(
inputStream
))
{
Sheet
sheet
=
workbook
.
getSheetAt
(
0
);
for
(
Row
row
:
sheet
)
{
// 跳过表头行
if
(
row
.
getRowNum
()
==
0
&&
isHeaderRow
(
row
))
{
continue
;
}
String
roadName
=
row
.
getCell
(
0
).
getStringCellValue
();
String
coordJson
=
row
.
getCell
(
1
).
getStringCellValue
();
String
linestring
=
parseToLinestring
(
coordJson
);
String
sql
=
String
.
format
(
"INSERT INTO road (name, geom) VALUES ('%s', ST_GeomFromText('%s', 4326));"
,
roadName
.
replace
(
"'"
,
"''"
),
linestring
);
sqlStatements
.
add
(
sql
);
}
}
return
sqlStatements
;
}
/**
* 判断是否为表头行
*/
private
boolean
isHeaderRow
(
Row
row
)
{
if
(
row
.
getCell
(
0
)
==
null
)
return
false
;
String
value
=
row
.
getCell
(
0
).
getStringCellValue
();
return
value
!=
null
&&
(
value
.
contains
(
"名称"
)
||
value
.
contains
(
"name"
)
||
value
.
contains
(
"路段"
)
||
value
.
contains
(
"road"
));
}
/**
* 解析JSON坐标为LINESTRING格式
* 注意:Excel中存储的坐标格式为 [经度, 纬度],但MySQL ST_GeomFromText需要纬度在前
* 因此这里会交换经纬度顺序
*/
private
String
parseToLinestring
(
String
json
)
{
String
trimmed
=
json
.
trim
();
if
(
trimmed
.
startsWith
(
"["
))
trimmed
=
trimmed
.
substring
(
1
);
if
(
trimmed
.
endsWith
(
"]"
))
trimmed
=
trimmed
.
substring
(
0
,
trimmed
.
length
()
-
1
);
String
[]
points
=
trimmed
.
split
(
"\\],\\s*\\["
);
StringBuilder
sb
=
new
StringBuilder
(
"LINESTRING("
);
for
(
int
i
=
0
;
i
<
points
.
length
;
i
++)
{
String
clean
=
points
[
i
].
replaceAll
(
"[\\[\\]]"
,
""
);
String
[]
lngLat
=
clean
.
split
(
","
);
if
(
lngLat
.
length
<
2
)
continue
;
if
(
i
>
0
)
sb
.
append
(
", "
);
// 关键修改:交换经纬度顺序,纬度在前,经度在后
// Excel中是 [经度, 纬度],MySQL需要 (纬度 经度)
String
longitude
=
lngLat
[
0
].
trim
();
String
latitude
=
lngLat
[
1
].
trim
();
sb
.
append
(
latitude
).
append
(
" "
).
append
(
longitude
);
}
sb
.
append
(
")"
);
return
sb
.
toString
();
}
}
\ No newline at end of file
src/main/java/com/example/pipelinenetwork/util/LinestringUtil.java
0 → 100644
View file @
b74bcf3f
package
com.example.pipelinenetwork.util
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
public
class
LinestringUtil
{
public
static
List
<
List
<
Double
>>
parseLinestring
(
String
linestring
)
{
List
<
List
<
Double
>>
result
=
new
ArrayList
<>();
Pattern
pattern
=
Pattern
.
compile
(
"LINESTRING\\((.*)\\)"
);
Matcher
matcher
=
pattern
.
matcher
(
linestring
);
if
(
matcher
.
find
())
{
String
coordsPart
=
matcher
.
group
(
1
);
String
[]
coordPairs
=
coordsPart
.
split
(
","
);
for
(
String
pair
:
coordPairs
)
{
String
[]
xy
=
pair
.
trim
().
split
(
"\\s+"
);
double
x
=
Double
.
parseDouble
(
xy
[
0
]);
double
y
=
Double
.
parseDouble
(
xy
[
1
]);
List
<
Double
>
point
=
new
ArrayList
<>();
point
.
add
(
x
);
point
.
add
(
y
);
result
.
add
(
point
);
}
}
return
result
;
}
}
\ No newline at end of file
src/main/resources/application.yml
0 → 100644
View file @
b74bcf3f
server
:
port
:
18720
spring
:
datasource
:
url
:
jdbc:mysql://192.168.10.3:3307/pipeline?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
username
:
root
password
:
ruzhoukj@2022
driver-class-name
:
com.mysql.cj.jdbc.Driver
mybatis
:
configuration
:
map-underscore-to-camel-case
:
true
src/main/resources/com/example/pipelinenetwork/mapper/RoadMapper.xml
0 → 100644
View file @
b74bcf3f
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
namespace=
"com.example.pipelinenetwork.mapper.RoadMapper"
>
<select
id=
"matchNearestRoad"
resultType=
"java.util.Map"
>
SELECT
id,
name,
ST_AsText(geom) AS geomWkt,
ST_Distance(geom, ST_SRID(POINT(#{lng}, #{lat}), 4326)) AS distance
FROM
road
WHERE
ST_Distance(geom, ST_SRID(POINT(#{lng}, #{lat}), 4326))
<
50
ORDER BY
distance ASC
LIMIT 1
</select>
</mapper>
Prev
1
2
Next
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