Skip to content

Commit d07dc03

Browse files
blvyoucan何翀彬
andauthored
feat: GeocodingX增加save方法,以方便生成自定义的dat字典文件 #180 (#181)
Co-authored-by: 何翀彬 <[email protected]>
1 parent b2ead48 commit d07dc03

File tree

6 files changed

+103
-3
lines changed

6 files changed

+103
-3
lines changed

src/main/java/org/bitlap/geocoding/GeocodingX.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,8 @@ open class GeocodingX(val ctx: Context) {
109109
indexBuilder.indexRegions(listOf(region), replace)
110110
return this
111111
}
112+
113+
fun save(path: String) {
114+
ctx.persister.save(path)
115+
}
112116
}

src/main/java/org/bitlap/geocoding/core/AddressPersister.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ interface AddressPersister {
2525
* 新增一个region信息
2626
*/
2727
fun addRegionEntity(entity: RegionEntity)
28+
29+
/**
30+
* 保存一个新的dat文件
31+
*/
32+
fun save(path: String)
2833
}

src/main/java/org/bitlap/geocoding/core/RegionCache.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package org.bitlap.geocoding.core;
1+
package org.bitlap.geocoding.core
22

3-
import org.bitlap.geocoding.model.RegionEntity;
3+
import org.bitlap.geocoding.model.RegionEntity
44

55
/**
66
* Desc: 获取 region entity 的抽象接口
@@ -26,4 +26,9 @@ interface RegionCache {
2626
* 新增一个region信息
2727
*/
2828
fun addRegionEntity(entity: RegionEntity)
29+
30+
/**
31+
* 保存一个新的dat文件
32+
*/
33+
fun save(path: String)
2934
}

src/main/java/org/bitlap/geocoding/core/impl/DefaultAddressPersister.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,10 @@ open class DefaultAddressPersister (
3737
regionCache.addRegionEntity(entity)
3838
}
3939

40+
/**
41+
* 保存一个新的dat文件
42+
*/
43+
override fun save(path: String) {
44+
regionCache.save(path)
45+
}
4046
}

src/main/java/org/bitlap/geocoding/core/impl/DefaultRegoinCache.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import org.bitlap.geocoding.core.RegionCache
55
import org.bitlap.geocoding.model.RegionEntity
66
import org.bitlap.geocoding.model.RegionType
77
import java.io.ByteArrayInputStream
8-
import java.util.Base64
8+
import java.io.ByteArrayOutputStream
9+
import java.io.FileOutputStream
10+
import java.util.*
911
import java.util.zip.GZIPInputStream
12+
import java.util.zip.GZIPOutputStream
13+
import kotlin.text.Charsets.UTF_8
1014

1115
/**
1216
* Desc: 默认 [RegionEntity] 获取的缓存类
@@ -76,4 +80,16 @@ open class DefaultRegionCache(dataClassPath: String) : RegionCache {
7680
this.REGION_CACHE[entity.id] = entity
7781
this.REGION_CACHE[entity.parentId]?.children?.add(entity)
7882
}
83+
84+
/**
85+
* 保存一个新的dat文件
86+
*/
87+
override fun save(path: String) {
88+
val gzip = ByteArrayOutputStream()
89+
GZIPOutputStream(gzip, 8192).use { gzipos ->
90+
gzipos.write(Gson().toJson(regions, RegionEntity::class.java).toByteArray(UTF_8))
91+
}
92+
val dat = Base64.getMimeEncoder().encode(gzip.toByteArray())
93+
ByteArrayInputStream(dat).copyTo(FileOutputStream(path), 8192)
94+
}
7995
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.bitlap.geocoding
2+
3+
import org.bitlap.geocoding.model.Address
4+
import org.bitlap.geocoding.model.RegionType
5+
import org.junit.Test
6+
import kotlin.test.assertEquals
7+
8+
/**
9+
* Desc: 测试保存自定义文件
10+
11+
* Created by hechongbin
12+
* Date: 2023/6/7
13+
*/
14+
class TestCustomDatSave {
15+
16+
@Test
17+
fun saveAndNomalizing() {
18+
val geocoding = GeocodingX("region_2021.dat")
19+
val addrss = "浙江省杭州市临平区经济开发区新颜路22号501D"
20+
21+
// 未添加自定义地区"临平区"
22+
assertEquals(
23+
geocoding.normalizing(addrss),
24+
Address(
25+
330000000000, "浙江省",
26+
330100000000, "杭州市",
27+
330110000000, "余杭区",
28+
330110001000, "临平街道",
29+
null, null,
30+
null, null,
31+
null, null,
32+
"501",
33+
"区经济开发区新颜路22号D"
34+
)
35+
)
36+
37+
// 添加自定义地区"临平区"
38+
geocoding.addRegionEntry(330113000000, 330100000000, "临平区", RegionType.District, "", true)
39+
40+
val addNew = Address(
41+
330000000000, "浙江省",
42+
330100000000, "杭州市",
43+
330113000000, "临平区",
44+
null, null,
45+
null, null,
46+
null, null,
47+
"新颜路", "22号",
48+
"501",
49+
"D"
50+
)
51+
52+
assertEquals(geocoding.normalizing(addrss), addNew)
53+
54+
// 添加后"临平区"后保存自定义字典文件
55+
val filename = "mydata.dat"
56+
val filePath = "${this.javaClass.classLoader.getResource("").path}/$filename"
57+
geocoding.save(filePath)
58+
59+
// 读取添加了"临平区"的自定义字典文件
60+
val myGeocoding = GeocodingX(filename)
61+
assertEquals(myGeocoding.normalizing(addrss), addNew)
62+
}
63+
64+
}

0 commit comments

Comments
 (0)