Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to import data into mysql database in geojson

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report