Skip to main content
Question

Change the geometry structure in Mongo

  • December 7, 2017
  • 3 replies
  • 18 views

fmeuser_gc
Contributor
Forum|alt.badge.img+9

Trying to write from SQL server to MongoDB.

It has a below structure:

Geometry

Type

 

Coords

 

[

 

[

 

[

 

[long, late], (pt 1)

 

[long, late], (pt 2)

 

]

 

]

 

]

 

 

Can we store all the polygon data like the following:

 

 

Geometry:

 

Cords:

 

[

 

[long, late], (pt 1)

 

[long, late], (pt 2)

 

]

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

3 replies

jakemolnar
Forum|alt.badge.img
  • December 7, 2017

The GeoJSON specification for the type "Polygon" requires its coordinates to be an array of linear rings (see the spec here).

So we must make geometry like this:

{
  "type": "Polygon",
  "coordinates":
    [                           <--- this array is an array of linear rings
      [                         <--- this array is a linear ring
        [long0, lat0],          <--- this array is a point
        ...,
        [longN, latN]
      ]
    ]
}

GeoJSON requires this so that Polygons can be Donuts, ie. so that they can have holes. For example, to make a Polygon that looks like this:

4  ___________
  |/ /_/_/_/ /|                <--- the shaded areas are part of the polygon
  | /|     |/ |
  |/ |_____| /|
  |_/_/_/_/_/_|
0             4

We would make a GeoJSON polygon like this:

{
  "type": "Polygon",
  "coordinates": [
    [ [0,0], [4,0], [4,4], [0,4], [0,0] ],       <--- first ring is exterior
    [ [1,1], [1,3], [3,3], [3,1], [1,1] ]        <--- next ring is interior
  ]
}

Maybe if you are just trying to access the exterior ring, then your JavaScript can be something like:

if (geometry.coordinates.length > 0) {
   var exterior_ring = geometry.coordinates[0];
   // do something with ring...
}

fmeuser_gc
Contributor
Forum|alt.badge.img+9
  • Author
  • Contributor
  • January 5, 2018

The GeoJSON specification for the type "Polygon" requires its coordinates to be an array of linear rings (see the spec here).

So we must make geometry like this:

{
  "type": "Polygon",
  "coordinates":
    [                           <--- this array is an array of linear rings
      [                         <--- this array is a linear ring
        [long0, lat0],          <--- this array is a point
        ...,
        [longN, latN]
      ]
    ]
}

GeoJSON requires this so that Polygons can be Donuts, ie. so that they can have holes. For example, to make a Polygon that looks like this:

4  ___________
  |/ /_/_/_/ /|                <--- the shaded areas are part of the polygon
  | /|     |/ |
  |/ |_____| /|
  |_/_/_/_/_/_|
0             4

We would make a GeoJSON polygon like this:

{
  "type": "Polygon",
  "coordinates": [
    [ [0,0], [4,0], [4,4], [0,4], [0,0] ],       <--- first ring is exterior
    [ [1,1], [1,3], [3,3], [3,1], [1,1] ]        <--- next ring is interior
  ]
}

Maybe if you are just trying to access the exterior ring, then your JavaScript can be something like:

if (geometry.coordinates.length > 0) {
   var exterior_ring = geometry.coordinates[0];
   // do something with ring...
}
Hi @jakemolnar, we are not using JavaScript. Is there any workaround please?

 


jakemolnar
Forum|alt.badge.img
  • January 5, 2018
Hi @jakemolnar, we are not using JavaScript. Is there any workaround please?

 

Hey @fmeuser_gc, then what are you using to access the Polygon data?

 

 

Anyway, one workaround I can think of is to convert your polygons to lines (https://knowledge.safe.com/questions/4730/how-to-convert-polygons-to-lines-and-vice-versa.html), since lines have the representation you are looking for:

 

 

{
  "type": "LineString",
  "coordinates": [ 
    [ long0, lat0 ],
    ...,
    [ longN, latN ]
  ]