Persisting GeoSpatial Data in MongoDB
anapitalai photos

Persisting GeoSpatial Data in MongoDB

Persisting data is crucial in web applications, if data is not saved, the data is wiped out when a page refresh is done on the web browser, caching in the browser can be a way to mitigate the problem but that data still is temporary. Permanent storage is made possible by using a database or a text file or excel spreadsheet. A relational database management system is the way to go when you are serious about saving huge data over a long period of time.

When exploring database systems, you will come across in-memory, sql and no-sql databases. With SQL databases, data that is saved must conform to a specfic model that's created when designing the tables and that model resticts users from saving data fields that are not specified in that model, eg: When I do an insert into the names tables, that table has a name and password field but if I try to add additional data fields not in the model, the database will complain or restrict me from doing so. The data is saved in a tabular form.

NO-SQL database uses BSON, a format similiar to JSON format where data is saved as a key value pair and also has model implementation similar to SQL databases.

Our task was to save geospatial data in GEOJSON format into a mongo database, where data is uploaded from a user interface. We had to search the web for examples on how to model the geojson data in the below format. Below is a geojson multipolygon data.

{

?"type": "FeatureCollection",

?"features": [

??{

???"type": "Feature",

???"properties": {

????"Id": 37,

????"gridcode": 5

???},

???"geometry": {

????"type": "MultiPolygon",

????"coordinates": [

?????[[[

????????147.8616691031514,

????????-6.618262516985316

???????],?[

????????147.8616691501913,

????????-6.618289652217093

???????],?[

????????147.86169628700546,

????????-6.618289605179121

???????],??[

????????147.86169633404702,

????????-6.618316740410674

???????],??[

????????147.8616964751729,

????????-6.618398146105075

???????],[

????????147.86166933835275,

????????-6.618398193143824

???????],[

????????147.8616692913121,

????????-6.618371057912181

???????],??[

????????147.86164215449324,

????????-6.618371104949259

???????],[

????????147.86164196633848,

????????-6.618262564021611

???????],[

????????147.8616691031514,

????????-6.618262516985316

???????]]]]}},{

???"type": "Feature",

???"properties": {

????"Id": 37,

????"gridcode": 5,

???},

???"geometry": {

????"type": "MultiPolygon",

????"coordinates": [

?????[??[?[

????????147.86175098404107,

????????-6.618533728177976

???????],[

????????147.86175103108735,

????????-6.618560863408854

???????],?[

????????147.86177816791576,

????????-6.618560816364493

???????],[

????????147.8617782620119,

????????-6.618615086825756

???????],[

????????147.86180539884307,

????????-6.618615039779525

???????],?[

????????147.86180568114509,

????????-6.618777851161481

???????],?[

????????147.86175140746477,

????????-6.618777945254797

???????],[

????????147.86175131336924,

????????-6.618723674793502

???????],?[

????????147.8617241765317,

????????-6.61872372183755

???????],[

????????147.86172408243993,

????????-6.61866945137573

???????],?[

????????147.86172403539433,

????????-6.618642316144777

???????],[

????????147.86169689856106,

????????-6.61864236318676

???????],[

????????147.8616968044734,

????????-6.618588092724355

???????],?[

????????147.86172394130372,

????????-6.618588045682761

???????],[

????????147.86172389425872,

????????-6.618560910451735

???????],?[

????????147.8617238472139,

????????-6.61853377522066

???????],[

????????147.86175098404107,

????????-6.618533728177976

???????]??]]]}}

An this is what we ended up with enabling us to save a multipolygon geojson file into the database and querying to from the user interface.

const geometrySchema = new mongoose.Schema({

type: {

type: String,

enum: ['Polygon'],

required: false

},

coordinates: {

type: [[[Number]]],

index:'2dsphere',

required: false

}

});

const propertiesSchema=new mongoose.Schema({

type:String,

Id: {

type: String,

required:false

},

gridcode:{

type:String

}

})

const featureObjectSchema=new mongoose.Schema({

type:{

type:String,

enum:['Feature'],

default:'Feature'

},

properties:{type:propertiesSchema},

geometry:{type:geometrySchema}

})

const CoralSchema = mongoose.Schema({

createdAt: {

type: Date,

default: Date.now

},

name:{

type:String

},

type:{

type:String,

enum:['FeatureCollection'],

default:'FeatureCollection'

},

features:{

type:{}

}

});

Entries in the mongo Database replicating the the geojson format above.

No alt text provided for this image
anapitalai photos

要查看或添加评论,请登录

Alois Napitalai的更多文章

  • Using the Terminal in Ubuntu Server

    Using the Terminal in Ubuntu Server

    A graphical User Interface(GUI) makes it easy for us to navigate and do work especially in the Ubuntu desktop version…

    3 条评论
  • Logistic Regression

    Logistic Regression

    This is a follow up tutorial on my previous post linear regression on my road to understanding machine learning. As a…

    8 条评论
  • Road to Understanding Machine Learning

    Road to Understanding Machine Learning

    Traditional Machine Learning-Linear Regression Algorithm Machine learning is simply training a machine to make…

  • Automate a Full-stack Application Deployment Using GitHub Actions

    Automate a Full-stack Application Deployment Using GitHub Actions

    #githubactions #git #reactjs #expressjs #virtualization #fullstackdevelopment #githubrepository #statemanagement I have…

    2 条评论
  • Using Github Actions For Website Building

    Using Github Actions For Website Building

    name: Website Deployment Automation on: push jobs: installs: runs-on: ubuntu-latest…

    2 条评论
  • Excel Functions and Formulas

    Excel Functions and Formulas

    I got stuck on excel formulas and functions the other day, it took me some time to get what I wanted. I have a little…

  • React and Ionic Routing

    React and Ionic Routing

    React Routing What is routing in react? Routing in React is the process of mapping URLs(uniform resource locators) to…

  • Under the Hood of React Components

    Under the Hood of React Components

    Doing It The JSX Way Components are the building blocks of react websites and UIs and these components are built using…

  • Web Proxy Authentication

    Web Proxy Authentication

    In my last article, I wrote about the installation of squid as a caching server that can be used to locally cache pages…

    7 条评论
  • Squid Cache Web Proxy

    Squid Cache Web Proxy

    Many computer networks tend to crawl when there are many users accessing the internet, or there are unwanted traffic…

    4 条评论

社区洞察

其他会员也浏览了