Avishai
Avishai

Reputation: 4732

Can't save polygon with PostGIS/RGeo

How do you save a polygon using the PostGIS adapter for Rails? In my model I have:

self.rgeo_factory_generator = RGeo::Geos.factory_generator
set_rgeo_factory_for_column(:bounds, RGeo::Geographic.spherical_factory(:srid => 4326))

but then when I try to save a polygon to the DB, it doesn't raise any errors, but always fails to store the polygon:

1.9.3p194 :011 > n.bounds = "POLYGON(-149.737965876574 61.1952881991104, -149.71848377896 61.1953198415937, -149.718483761252 61.1952938698801, -149.718483872402 61.1951924591105)"
 => "POLYGON(-149.737965876574 61.1952881991104, -149.71848377896 61.1953198415937, -149.718483761252 61.1952938698801, -149.718483872402 61.1951924591105)" 
1.9.3p194 :012 > n.save
   (0.2ms)  BEGIN
   (0.3ms)  COMMIT
 => true 
1.9.3p194 :013 > n.bounds
 => nil 

How can I get this to work?

Upvotes: 2

Views: 2011

Answers (2)

Mike T
Mike T

Reputation: 43642

It's invalid WKT and cannot be parsed for two reasons:

  1. There is no ring. You need to add another set of parenthesis around the coordinate array.
  2. The ring is not closed. Repeat the starting point as the end point.

This is what the valid WKT should look like:

POLYGON((-149.737965876574 61.1952881991104, -149.71848377896 61.1953198415937, -149.718483761252 61.1952938698801, -149.718483872402 61.1951924591105, -149.737965876574 61.1952881991104))

Upvotes: 6

dimuch
dimuch

Reputation: 12818

In pure PostGIS this polygon will generate geometry contains non-closed rings error. Try to "close" the polygon (add first point at the end)

Upvotes: 2

Related Questions