Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Segmentize for GeosCapi geometries. #364

Merged
merged 9 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ RGeo works with the following Ruby implementations:

Some features also require the following:

* GEOS 3.2 or later is highly recommended. (3.3.3 or later preferred.) Some
functions will not be available without it. This C/C++ library may be
* GEOS 3.10 or later is highly recommended. Some functions will not be available without it. This C/C++ library may be
available via your operating system's package manager (`sudo aptitude
install libgeos-dev` for debian based Linux distributions, `yum install geos geos-devel` for redhat based Linux distributions), or you can
download it from http://trac.osgeo.org/geos
Expand Down
23 changes: 23 additions & 0 deletions ext/geos_c_impl/geometry.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,27 @@ method_geometry_buffer(VALUE self, VALUE distance)
return result;
}

static VALUE
method_geometry_segmentize(VALUE self, VALUE max_segment_length)
{
VALUE result;
RGeo_GeometryData* self_data;
const GEOSGeometry* self_geom;
VALUE factory;

result = Qnil;
self_data = RGEO_GEOMETRY_DATA_PTR(self);
self_geom = self_data->geom;
if (self_geom) {
factory = self_data->factory;
result = rgeo_wrap_geos_geometry(
factory,
GEOSDensify(self_geom, rb_num2dbl(max_segment_length)),
Qnil);
}
return result;
}

static VALUE
method_geometry_buffer_with_style(VALUE self,
VALUE distance,
Expand Down Expand Up @@ -1308,6 +1329,8 @@ rgeo_init_geos_geometry()
geos_geometry_methods, "make_valid", method_geometry_make_valid, 0);
rb_define_method(
geos_geometry_methods, "polygonize", method_geometry_polygonize, 0);
rb_define_method(
geos_geometry_methods, "segmentize", method_geometry_segmentize, 1);
}

RGEO_END_C
Expand Down
7 changes: 7 additions & 0 deletions test/geos_capi/line_string_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ def test_polygonize_dangle

assert_equal expected, input.polygonize
end

def test_segmentize
input = @factory.parse_wkt("LINESTRING(0 0, 0 10)")
expected = @factory.parse_wkt("LINESTRING (0 0, 0 5, 0 10)")

assert_equal expected, input.segmentize(5)
end
end
7 changes: 7 additions & 0 deletions test/geos_capi/polygon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,11 @@ def test_polygonize

assert_equal expected, input.polygonize
end

def test_segmentize
input = @factory.parse_wkt("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))")
expected = @factory.parse_wkt("POLYGON ((0 0, 5 0, 10 0, 10 5, 10 10, 5 10, 0 10, 0 5, 0 0))")

assert_equal expected, input.segmentize(5)
end
end