Skip to main content
Question

Change the geometry structure in Mongo


fmeuser_gc
Contributor
Forum|alt.badge.img+8

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)

 

]

3 replies

jakemolnar
Forum|alt.badge.img

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+8
  • Author
  • Contributor
  • January 5, 2018
jakemolnar wrote:

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
fmeuser_gc wrote:
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 ]
  ]

Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings