Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to import data into mysql database in geojson

Shulou Source: shulou.com Published: 2022-06-02 06:53:21 10月03日 Update

This article is about how to import the data into the mysql database in geojson. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Use nodejs to import geojson spatial layer data into mysql database.

Specific implementation code: / / reference the class libraries of fs, mysql and Geojson2Wkt

Var fs = require ("fs")

Var mysql = require ('mysql')

/ / Geojson2Wkt is to convert the coordinates in geojson into wkt.

Var Geojson2wkt = require ('Geojson2Wkt')

/ / set the connection information for mysql

Var connection = mysql.createConnection ({

Host: 'localhost'

User: 'root'

Password: '123456'

Database: 'databasename'

});

/ / Open database connection

Connection.connect ()

Var sql = []

Fs.readFile ('geojson layer file', {

Encoding: "utf-8"

}, (err, data) = > {

Let jsonobj = JSON.parse (data)

Var arr = jsonobj.features

Var brr = []

Var crr = []

Var drr = []

Var data = []

Var k = 0

Var sqla

/ / convert geojson coordinates to wkt form

For (var I = 0; I < arr.length; iTunes +) {

Brr [I] = arr [I].

Drr [I] = ARR [I] .properties

Crr [I] = Geojson2wkt.convert (brr [I])

}

For (var j in drr [0]) {

Kicking +

Data [k] = j

}

/ / get the space type of geojson data. Mysql needs to set fields according to different space types.

Var tdata = arr [0] .geometry.type

Tdata = tdata.toLowerCase ()

/ / create sql statements for mysql data tables

Sqla = "CREATE TABLE IF NOT EXISTS `tablename` (`" + data [1] + "`tablename`,`" + data [2] + "`VARCHAR (100) NOT NULL, `" + data [3] + "`VARCHAR (100) NOT NULL,`" + data [4] + "`VARCHAR (100) NOT NULL, `" + data [5] + "`VARCHAR (100) NOT NULL, `geometry`" + tdata + "NOT NULL)"

Connection.query (sqla)

/ / concatenate sql statements to cycle through the data written to the layer

For (var t = 0; t < arr.length; tasking +) {

Sql [t] =

"insert into tablename (" + data [1] + "," + data [2] + "," + data [3] + "," + data [4] + "," + data [5] + ", geometry) values ('" + drr [t] [data [1]]) + ",'" + drr [t] [data [2]] + "," + "'" + drr [t] [data [3]] + ",'" + drr [t] [data [4]] + "' "+"'"+ drr [t] [data [5]] +"'"+", GeomFromText ('"+ crr [t] +"') "

Connection.query (SQL [t])

}

});

The above is how to import the data into the mysql database in geojson. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Data database space coordinates form more knowledge articles types statements different practical code information fields that is work meetings datasheets files articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft macOS Shulou Technology Linux