Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate gis data #64

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dataset_writer

import (
vn_common "github.com/thanglequoc-vn-provinces/v2/common"
gis "github.com/thanglequoc-vn-provinces/v2/gis"
"strings"
"time"
)
Expand All @@ -12,7 +13,8 @@ type DatasetFileWriter interface {
administrativeUnits []vn_common.AdministrativeUnit,
provinces []vn_common.Province,
districts []vn_common.District,
wards []vn_common.Ward) error
wards []vn_common.Ward,
gisData gis.ProvinceGIS) error
}

func getFileTimeSuffix() string {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package helper

func Chunks(s string, chunkSize int) []string {
if len(s) == 0 {
return nil
}
if chunkSize >= len(s) {
return []string{s}
}
var chunks []string = make([]string, 0, (len(s)-1)/chunkSize+1)
currentLen := 0
currentStart := 0
for i := range s {
if currentLen == chunkSize {
chunks = append(chunks, s[currentStart:i])
currentLen = 0
currentStart = i
}
currentLen++
}
chunks = append(chunks, s[currentStart:])
return chunks
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package dataset_writer
import (
"bufio"
"fmt"
vn_common "github.com/thanglequoc-vn-provinces/v2/common"
"log"
"os"
"strings"
"time"

vn_common "github.com/thanglequoc-vn-provinces/v2/common"
gis "github.com/thanglequoc-vn-provinces/v2/gis"
)

type MssqlDatasetFileWriter struct {
Expand All @@ -24,12 +27,17 @@ const insertProvinceValueMsSqlTemplate string = "('%s',N'%s',N'%s',N'%s',N'%s','

const insertDistrictWardValueMsSqlTemplate string = "('%s',N'%s',N'%s',N'%s',N'%s','%s','%s',%d)"


const insertMsSQLGISTemplate string = "INSERT INTO vn_gis(code,level,bbox, gis_geom) VALUES "
const insertMsSQLGISValueTemplate string = "('%s','%s',geography::STGeomFromText('POLYGON((%s, %s, %s, %s, %s))', 4326), geography::STGeomFromText('MULTIPOLYGON(((%s)))', 4326));\n"

func (w *MssqlDatasetFileWriter) WriteToFile(
regions []vn_common.AdministrativeRegion,
administrativeUnits []vn_common.AdministrativeUnit,
provinces []vn_common.Province,
districts []vn_common.District,
wards []vn_common.Ward) error {
wards []vn_common.Ward,
gisData []gis.ProvinceGIS) error {

fileTimeSuffix := getFileTimeSuffix()
outputFilePath := fmt.Sprintf(w.OutputFilePath, fileTimeSuffix)
Expand Down Expand Up @@ -140,5 +148,70 @@ func (w *MssqlDatasetFileWriter) WriteToFile(
dataWriterMsSql.Flush()
fileMsSql.Close()

writeMsSQLGISDataToFile(gisData)

return nil
}

func writeMsSQLGISDataToFile(gisData []gis.ProvinceGIS) {
fileTimeSuffix := getFileTimeSuffix()
outputFilePath := fmt.Sprintf("./output/mssql_generated_ImportGISData_vn_units_%s.sql", fileTimeSuffix)
file, err := os.OpenFile(outputFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal("Unable to write to file", err)
panic(err)
}
defer file.Close()

dataWriter := bufio.NewWriter(file)
dataWriter.WriteString("/* === Vietnamese Provinces Database - GIS Dataset for Microsoft SQL Server === */\n")
dataWriter.WriteString(fmt.Sprintf("/* Created at: %s */\n", time.Now().Format(time.RFC1123Z)))
dataWriter.WriteString("/* Reference: https://github.com/ThangLeQuoc/vietnamese-provinces-database */\n")
dataWriter.WriteString("/* =============================================== */\n\n")

// ring order: bottom left -> going around -> bottom left again
for _, g := range gisData {
dataWriter.WriteString(insertMsSQLGISTemplate)
dataWriter.WriteString(fmt.Sprintf(insertMsSQLGISValueTemplate,
g.LevelId,
"province",
gis.ToCoordinateStr(g.BBox.BottomLeftLng, g.BBox.BottomLeftLat),
gis.ToCoordinateStr(g.BBox.TopLeftLng, g.BBox.TopLeftLat),
gis.ToCoordinateStr(g.BBox.TopRightLng, g.BBox.TopRightLat),
gis.ToCoordinateStr(g.BBox.BottomRightLng, g.BBox.BottomRightLat),
gis.ToCoordinateStr(g.BBox.BottomLeftLng, g.BBox.BottomLeftLat), // repeat the bottom left again to close the polygon ring,
toMsSQLMultiPolygon(g.Coordinates[0][0]),
),
)

// district in province
for _, gd := range g.Districts {
dataWriter.WriteString(insertMySQLGISTemplate)
dataWriter.WriteString(fmt.Sprintf(insertMsSQLGISValueTemplate,
gd.LevelId,
"district",
gis.ToCoordinateStr(gd.BBox.BottomLeftLng, gd.BBox.BottomLeftLat),
gis.ToCoordinateStr(gd.BBox.TopLeftLng, gd.BBox.TopLeftLat),
gis.ToCoordinateStr(gd.BBox.TopRightLng, gd.BBox.TopRightLat),
gis.ToCoordinateStr(gd.BBox.BottomRightLng, gd.BBox.BottomRightLat),
gis.ToCoordinateStr(gd.BBox.BottomLeftLng, gd.BBox.BottomLeftLat), // repeat the bottom left again to close the polygon ring,
toMsSQLMultiPolygon(gd.Coordinates[0][0]),
),
)
}
}

dataWriter.Flush()
}

func toMsSQLMultiPolygon(ringCoordinates gis.GisLinearRingCoordinate) string {
var sb strings.Builder
for _, p := range ringCoordinates.GisPoints {
sb.WriteString(gis.ToCoordinateStr(p.Longitude, p.Latitude))
sb.WriteString(",")
}
// repeat the first point to close the ring
firstPoint := ringCoordinates.GisPoints[0]
sb.WriteString(gis.ToCoordinateStr(firstPoint.Longitude, firstPoint.Latitude))
return sb.String()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ package dataset_writer
import (
"bufio"
"fmt"
vn_common "github.com/thanglequoc-vn-provinces/v2/common"
"log"
"os"
"strings"
"time"

vn_common "github.com/thanglequoc-vn-provinces/v2/common"
"github.com/thanglequoc-vn-provinces/v2/dataset_writer/dataset_file_writer/helper"
gis "github.com/thanglequoc-vn-provinces/v2/gis"
)

const insertProvinceOracleTemplate string = "\tINTO provinces(code,name,name_en,full_name,full_name_en,code_name,administrative_unit_id,administrative_region_id) VALUES('%s','%s','%s','%s','%s','%s',%d,%d)"

const insertOracleGISTemplate string = "INSERT INTO vn_gis(code,unit_level,bbox, temp_wkt) VALUES "
const insertOracleGISValueTemplate string = "('%s','%s', sde.st_geomfromtext('POLYGON((%s, %s, %s, %s, %s))', 4326) ,%s);\n"

type OracleDatasetFileWriter struct {
OutputFilePath string
}
Expand All @@ -20,7 +27,8 @@ func (w *OracleDatasetFileWriter) WriteToFile(
administrativeUnits []vn_common.AdministrativeUnit,
provinces []vn_common.Province,
districts []vn_common.District,
wards []vn_common.Ward) error {
wards []vn_common.Ward,
gisData []gis.ProvinceGIS) error {

fileTimeSuffix := getFileTimeSuffix()
outputFilePath := fmt.Sprintf(w.OutputFilePath, fileTimeSuffix)
Expand Down Expand Up @@ -138,5 +146,81 @@ func (w *OracleDatasetFileWriter) WriteToFile(
dataWriter.WriteString("-- END OF SCRIPT FILE --\n")
dataWriter.Flush()
file.Close()

writeOracleGISDataToFile(gisData)
return nil
}

func writeOracleGISDataToFile(gisData []gis.ProvinceGIS) {
fileTimeSuffix := getFileTimeSuffix()
outputFilePath := fmt.Sprintf("./output/oracle_generated_ImportGISData_vn_units_%s.sql", fileTimeSuffix)
file, err := os.OpenFile(outputFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal("Unable to write to file", err)
panic(err)
}
defer file.Close()

dataWriter := bufio.NewWriter(file)
dataWriter.WriteString("/* === Vietnamese Provinces Database - GIS Dataset for Oracle === */\n")
dataWriter.WriteString(fmt.Sprintf("/* Created at: %s */\n", time.Now().Format(time.RFC1123Z)))
dataWriter.WriteString("/* Reference: https://github.com/ThangLeQuoc/vietnamese-provinces-database */\n")
dataWriter.WriteString("/* =============================================== */\n\n")

// ring order: bottom left -> going around -> bottom left again
for _, g := range gisData {
dataWriter.WriteString(insertOracleGISTemplate)
dataWriter.WriteString(fmt.Sprintf(insertOracleGISValueTemplate,
g.LevelId,
"province",
gis.ToCoordinateStr(g.BBox.BottomLeftLng, g.BBox.BottomLeftLat),
gis.ToCoordinateStr(g.BBox.TopLeftLng, g.BBox.TopLeftLat),
gis.ToCoordinateStr(g.BBox.TopRightLng, g.BBox.TopRightLat),
gis.ToCoordinateStr(g.BBox.BottomRightLng, g.BBox.BottomRightLat),
gis.ToCoordinateStr(g.BBox.BottomLeftLng, g.BBox.BottomLeftLat), // repeat the bottom left again to close the polygon ring,
toOracleMultiPolygon(g.Coordinates[0][0]),
),
)

// district in province
for _, gd := range g.Districts {
dataWriter.WriteString(insertOracleGISTemplate)
dataWriter.WriteString(fmt.Sprintf(insertOracleGISValueTemplate,
gd.LevelId,
"district",
gis.ToCoordinateStr(gd.BBox.BottomLeftLng, gd.BBox.BottomLeftLat),
gis.ToCoordinateStr(gd.BBox.TopLeftLng, gd.BBox.TopLeftLat),
gis.ToCoordinateStr(gd.BBox.TopRightLng, gd.BBox.TopRightLat),
gis.ToCoordinateStr(gd.BBox.BottomRightLng, gd.BBox.BottomRightLat),
gis.ToCoordinateStr(gd.BBox.BottomLeftLng, gd.BBox.BottomLeftLat), // repeat the bottom left again to close the polygon ring,
toOracleMultiPolygon(gd.Coordinates[0][0]),
),
)
}
}

dataWriter.Flush()
}

func toOracleMultiPolygon(ringCoordinates gis.GisLinearRingCoordinate) string {
var template = "MULTIPOLYGON(((%s)))"

var sb strings.Builder
for _, p := range ringCoordinates.GisPoints {
sb.WriteString(gis.ToCoordinateStr(p.Longitude, p.Latitude))
sb.WriteString(",")
}
// repeat the first point to close the ring
firstPoint := ringCoordinates.GisPoints[0]
sb.WriteString(gis.ToCoordinateStr(firstPoint.Longitude, firstPoint.Latitude))
return formatToClobAppend(fmt.Sprintf(template, sb.String()))
}

// Insert into table (clob_column) values ( to_clob( 'chunk 1' ) || to_clob( 'chunk 2' ) );
func formatToClobAppend(gisStr string) string {
chunks := helper.Chunks(gisStr, 3000)
for i, v := range chunks {
chunks[i] = "to_clob('" + v + "')"
}
return strings.Join(chunks, " || ")
}
Loading