1

I have array of locations and i want to insert them as a polygon column in a postgres table.

I have a column area with type geometry(Polygon,4326)

    const area = [
            { lat: 6.517784, lng: 3.368254 },
            { lat: 6.522207, lng: 3.376356 },
            { lat: 6.525885, lng: 3.376322 },
            { lat: 6.526396, lng: 3.379497 },
            { lat: 6.524009, lng 3.383188 },
            { lat: 6.523668, lng: 3.398294 },
            { lat: 6.517431, lng: 3.402618 },
            { lat: 6.514458, lng: 3.404732 },
            { lat: 6.510535, lng: 3.398294 },
            { lat: 6.501794, lng: 3.397425 },
            { lat: 6.496016, lng: 3.395054 },
            { lat: 6.488874, lng: 3.392029 },
            { lat: 6.491006, lng: 3.389969 },
            { lat: 6.492456, lng: 3.385677 },
            { lat: 6.494162, lng: 3.384990 },
            { lat: 6.492968, lng: 3.378467 },
            { lat: 6.498554, lng: 3.376289 }
]

I'm transforming the above data into a string

   const transform = area.map(position => return `${position.lat} ${position.lng}`}).join(', ')

then make an inset query using bookshelf and knex-posgis

const body = Object.assign({}, attrs, {
         area: st.geomFromText(`POLYGON((${transform}))`, 4326)
    });

    return Model.forge(body)
        .save()
        .then((result) => {
            return done(null, result)
        })
        .catch((error) => {
            return done(error, null)
        })

I'm getting an error saying geometry contains non-closed rings not sure what's causing the error. Any help will be appreciated.

1 Answer 1

2

The polygon should end on the same point as the start. That is why say non-closed.

Check examples ST_MakePolygon

Just add the first point to the end.

const area = [
        { lat: 6.517784, lng: 3.368254 },
        { lat: 6.522207, lng: 3.376356 },
        { lat: 6.525885, lng: 3.376322 },
        { lat: 6.526396, lng: 3.379497 },
        { lat: 6.524009, lng 3.383188 },
        { lat: 6.523668, lng: 3.398294 },
        { lat: 6.517431, lng: 3.402618 },
        { lat: 6.514458, lng: 3.404732 },
        { lat: 6.510535, lng: 3.398294 },
        { lat: 6.501794, lng: 3.397425 },
        { lat: 6.496016, lng: 3.395054 },
        { lat: 6.488874, lng: 3.392029 },
        { lat: 6.491006, lng: 3.389969 },
        { lat: 6.492456, lng: 3.385677 },
        { lat: 6.494162, lng: 3.384990 },
        { lat: 6.492968, lng: 3.378467 },
        { lat: 6.498554, lng: 3.376289 },
        { lat: 6.517784, lng: 3.368254 }
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.