How to create an RGeo polygon in WKT Ruby when a polygon has errors

I am working with a MySQL database with polygons stored in WKT format. Many polygons in the database have duplicate points (for example, in the example below, the point -122.323502 47.600959 is repeated three times).

When trying to call RGeo :: Cartesian :: Factory.parse_wkt () on these polygons, the result is nil.

How can I create RGeo objects from these polygons without modifying the polygon data.

poly = "MULTIPOLYGON(((-122.362163 47.618641,-122.344621 47.592555,-122.332017 47.592458,-122.32748 47.59241,-122.326109 47.592652,-122.324738 47.592895,-122.323147 47.593478,-122.321412 47.59411,-122.320826 47.594984,-122.320669 47.596296,-122.321149 47.598627,-122.323502 47.600959,-122.323502 47.600959,-122.323502 47.600959,-122.324071 47.601688,-122.320757 47.601688,-122.32073 47.604262,-122.320767 47.607663,-122.320746 47.609703,-122.320723 47.611938,-122.320714 47.612812,-122.320772 47.614075,-122.320799 47.618495,-122.362163 47.618641)))"

parsed_poly = RGeo::Cartesian::Factory.new().parse_wkt(poly)

=>nil
+5
source share
2 answers

Try the following:

RGeo::Geos.factory(:srid => 4326).parse_wkt(wkt_string)
+6
source
polygon = RGeo::Geographic.spherical_factory.parse_wkt(params[:polygon])

works for me! I am using rails + mysql

At first I tried to create a place entry in Rails as follows:

@place = Place.new(params)

Btw, my places table is as follows:

`polygon` polygon DEFAULT NULL,
`latlon` point DEFAULT NULL,

, , rgeo latlon mysql, . latlon, . place.rb

self.rgeo_factory_generator = RGeo::Geos.method(:factory)
set_rgeo_factory_for_column(:latlon, RGeo::Geographic.spherical_factory)
set_rgeo_factory_for_column(:polygon, RGeo::Geographic.spherical_factory)

, .

rgeo/reographic/interface.rb

  # geometric operations. In particular:
  #
  # * Relational operators such as Feature::Geometry#intersects? are
  #   not implemented for most types.
  # * Relational constructors such as Feature::Geometry#union are
  #   not implemented for most types.
  # * Buffer, convex hull, and envelope calculations are not
  #   implemented for most types. Boundaries are available except for
  #   GeometryCollection.
  # * Length calculations are available, but areas are not. Distances
  #   are available only between points.
  # * Equality and simplicity evaluation are implemented for some but
  #   not all types.
  # * Assertions for polygons and multipolygons are not implemented.

OK. . , .

polygon = RGeo::Geographic.spherical_factory.parse_wkt(params[:polygon])
params[:polygon] = polygon
@place = Place.new(place_params)
@place.save

!

polygon = RGeo::Geographic.spherical_factory.parse_wkt(params[:polygon])
@place.polygon = polygon
@place.save

, , , ActiveRecord !

+1

All Articles