Created
July 29, 2012 10:52
-
-
Save lwoodson/3197539 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Playing with java topology suite in jruby | |
require 'java' | |
require 'rubygems' | |
require 'mvn:com.vividsolutions:jts' | |
java_import "com.vividsolutions.jts.geom.PrecisionModel" | |
java_import "com.vividsolutions.jts.geom.GeometryFactory" | |
java_import "com.vividsolutions.jts.geom.Coordinate" | |
java_import "com.vividsolutions.jts.geom.Point" | |
java_import "com.vividsolutions.jts.geom.LineString" | |
java_import "com.vividsolutions.jts.geom.LinearRing" | |
java_import "com.vividsolutions.jts.geom.Polygon" | |
java_import "com.vividsolutions.jts.geom.Coordinate" | |
java_import "com.vividsolutions.jts.geom.GeometryCollection" | |
pm = PrecisionModel.new PrecisionModel::FIXED | |
factory = GeometryFactory.new pm | |
p1 = factory.createPoint(Coordinate.new(0,0)) | |
p2 = factory.createPoint(Coordinate.new(1,1)) | |
p1.distance p2 | |
linear_ring = factory.create_linear_ring( | |
[ | |
Coordinate.new(0, 0), | |
Coordinate.new(10, 0), | |
Coordinate.new(10, 10), | |
Coordinate.new(0, 10), | |
Coordinate.new(0, 0) | |
].to_java('com.vividsolutions.jts.geom.Coordinate') | |
) | |
polygon1 = factory.create_polygon linear_ring, nil | |
polygon1.valid? | |
polygon1.area | |
polygon1.contains p1 | |
polygon1.contains p2 | |
linear_ring = factory.create_linear_ring( | |
[ | |
Coordinate.new(5, 5), | |
Coordinate.new(15, 5), | |
Coordinate.new(15, 15), | |
Coordinate.new(5, 15), | |
Coordinate.new(5, 5) | |
].to_java('com.vividsolutions.jts.geom.Coordinate') | |
) | |
polygon2 = factory.create_polygon linear_ring, nil | |
polygon2.valid? | |
polygon2.area | |
polygon2.contains p1 | |
polygon2.contains p2 | |
polygon1.overlaps? polygon2 | |
polygon1.covers? polygon2 | |
polygon3 = polygon1.union polygon2 | |
polygon3.get_coordinates | |
polygon3.area | |
polygon1.within polygon3 | |
polygon2.within polygon3 | |
polygon4 = polygon1.difference polygon2 | |
polygon4.get_coordinates | |
polygon4.area | |
polygon4.within? polygon3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment