I want to draw points there poly-lines intersects in arcgis using arcgis java apie from esri.
I have written such code which checks whether to lines intersects
Polyline polyline = new Polyline();
polyline.startPath(118.169, 34.016);
polyline.lineTo(120.941, 39.7072);
SimpleLineSymbol symbol = new SimpleLineSymbol(Color.MAGENTA, 8);
symbol.setStyle(SimpleLineSymbol.Style.SOLID);
Graphic graphic = new Graphic(polyline, symbol);
graphicsLayer.addGraphic(graphic);
Polyline polyline2 = new Polyline();
polyline2.startPath(150.169, 34.016);
polyline2.lineTo(90.941, 39.7072);
SimpleLineSymbol symbol2 = new SimpleLineSymbol(Color.BLACK, 8);
symbol2.setStyle(SimpleLineSymbol.Style.SOLID);
Graphic graphic2 = new Graphic(polyline2, symbol2);
graphicsLayer.addGraphic(graphic2);
Polyline intersection = (Polyline)GeometryEngine.intersect(polyline, polyline2, srMap);
System.out.println(intersection.getPointCount());
System.out.println(GeometryEngine.intersects(polyline, polyline2, srMap));
first print line returns 0 but the second print line returns true. How I can get thouse intersection points in arcgis?
Related
I wanna make an ellipse mask for cropping an image so only the contents inside of the ellipse will be shown.
Could you inspect my code?
public static Mat cropImage(Mat imageOrig, MatOfPoint contour){
Rect rect = Imgproc.boundingRect(contour);
MatOfPoint2f contour2f = new MatOfPoint2f(contour.toArray());
RotatedRect boundElps = Imgproc.fitEllipse(contour2f);
Mat out = imageOrig.submat(rect);
// the line function is working
Imgproc.line(out, new Point(0,0), new Point(out.width(), out.height()), new Scalar(0,0,255), 5);
// but not this one
Imgproc.ellipse(out, boundElps, new Scalar(255, 0, 0), 99);
return out;
}//cropImage
It seems like it's not working at all. Though you can see the line function I've done to test if it is working on the right image and I can see a line but there's no ellipse.
Here's a sample output of my cropImage function.
TIA
You're retrieving the ellipse coordinates in the imageOrig coordinates system, but you're showing it on the cropped out image.
If you want to show the ellipse on the crop, you need to translate the ellipse center to account for the translation introduced by the crop (top-left coordinates of rect), something like:
boundElps.center().x -= rect.x; boundElps.center().y -= rect.y;
You can try this out:
RotatedRect rRect = Imgproc.minAreaRect(contour2f);
Imgproc.ellipse(out, rRect , new Scalar(255, 0, 0), 3);
You should check for the minimum requirements for using the fitEllipse as shown in this post.
The function fitEllipse requires atleast 5 points.
Note: Although the reference I mention is for Python, I hope you can do the same for Java.
for cnt in contours:
area = cv2.contourArea(cnt)
# Probably this can help but not required
if area < 2000 or area > 4000:
continue
# This is the check I'm referring to
if len(cnt) < 5:
continue
ellipse = cv2.fitEllipse(cnt)
cv2.ellipse(roi, ellipse, (0, 255, 0), 2)
Hope it helps!
I am currently using OpenCV to create a system which detects whether vehicles are in certain zones. So far, I've got to the point where a Rect is drawn around each vehicle. My next step is to find the central points of these rectangles and see whether that falls within a particular zone.
I realise that the best way of doing this is probably to find the coordinates of the Rect's four corners and then take an average x and average y-coordinate to find the coordinates for the central point. However, I'm not sure how to do this. Is there some function which lets me access OpenCV's Rect coordinates directly?
Edit to original: As was pointed out, we cannot use '+' operator directly on tl() and br().
Use this instead to get the center of a rectangle:
Point p1 = new Point(100, 100);
Point p2 = new Point(600, 800);
Rect myrect = new Rect(p1, p2);
System.out.println(String.format("Rectangle: %s", myrect));
Point centroid = new Point(myrect.x + 0.5*(myrect.width), myrect.y + 0.5*(myrect.height));
System.out.println(String.format("centroid: %s", centroid));
This prints:
Rectangle: {100, 100, 500x700}
centroid: {350.0, 450.0}
Old answer:
[Use rect's methods:
tl ()
br ()
to get the top left and bottom right points, respectively.]
If you did want to use tl, br methods you can do:
Point anotherCentroid = new Point(0.5 * (myrect.br().x + myrect.tl().x), 0.5 * (myrect.br().y + myrect.tl().y));
i want to trace the trajectory between differents points
for the test i creat points and try to link between these points
this is my code
OpenStreetMapLayer osm = new OpenStreetMapLayer();
map.addLayer(vectorLayer);
List<Point>points= new ArrayList<Point>();
Point point = new Point(44.272872,4.27826);
Point point2 = new Point(-55.272873,5.3873837);
Point point3 = new Point(5.272873,54.3873837);
points.add(point);
points.add(point2);
points.add(point3);
Point[] coord=new Point[points.size()];
points.toArray(coord);
polyline.setPoints(coord);
vectorLayer.addComponent(polyline);
Style defaultstyle = new Style();
/* Set stroke color to green, otherwise like default style */
defaultstyle.extendCoreStyle("default");
defaultstyle.setStrokeColor("#0000ff");
defaultstyle.setStrokeWidth(3);
defaultstyle.setFillColor("#adfffc");
defaultstyle.setFillOpacity(0.4);
// Make borders of selected graphs bigger
Style selectStyle = new Style();
selectStyle.setStrokeWidth(5);
StyleMap stylemap = new StyleMap(defaultstyle, defaultstyle, null);
// make selectStyle inherit attributes not explicitly set
stylemap.setExtendDefault(true);
vectorLayer.setStyleMap(stylemap);
but when i execute my code i get just a point i've asked they told me this point is the point of cordinate(0,0)
this is the screen catch of the set of points without ZOOM (the blue point)
http://img4.hostingpics.net/pics/810776sss.png
and this is the MAX ZOOM
http://img4.hostingpics.net/pics/122823ert.png
i want to know if it is a probleme of scale or what?
thanks in advance
You are using https://en.wikipedia.org/wiki/EPSG:4326 coords but OSM is using https://wiki.openstreetmap.org/wiki/EPSG:3857. The first one is in abs(180,90) where the second one is in in abs(6356752,6378137). so your points are basically at the center in spherical mercator and zooming very close will give your result. you have to convert your data e.g. using geotools
I basicly want to replicate the following code snippet (Y-Axis is up) to libgdx:
let archer = Vector3d(1.0,0.0,1.0)
let target = Vector3d(4.0,0.0,5.0)
let travel = target - archer
let transform = Matrix4d.CreateTranslation(-archer) *
Matrix4d.CreateRotationY(Math.Atan2(travel.Z,travel.X))
Vector3d.Transform(archer, transform) // transforms archer to (0,0,0)
Vector3d.Transform(target, transform) // transforms target to (5,0,0)
source:
https://gamedev.stackexchange.com/questions/33901/how-to-make-an-arrow-land-at-a-specific-position-in-3d-world-space
And here is my approach(Z-Axis is up):
archer = new Vector3(1,1,0);
target = new Vector3(4,5,0);
Vector3 travel = new Vector3(target).sub(new Vector3(archer));
float degree = (float) Math.toDegrees(Math.atan2(travel.y, travel.x));
Matrix4 transform = new Matrix4().setToTranslation(new Vector3(archer).mul(-1));
transform.mul(new Matrix4().rotate(0, 0, 1, degree));//in my case Z-Axis is up !
archer.mul(transform); //-1.2,0.39999998,0 should be: 0,0,0
target.mul(transform); //-2.6000001,5.2,0.0 should be: 5,0,0
I am getting wrong results and i can't find my mistake but i guess it has something to do with the rotate part in line 6
I suppose it should be
archer = new Vector3(1,0,1);
target = new Vector3(4,0,5);
I have a Line2D and a Area object and I want the intersecting Line2D. The result can be also a GeneralPath. How can I do this?
You can use the method instersects in class Area. Line2D could be replaced by a Rectangle2D though.
Second chance :
build your line2D, it is a shape.
Build an area around it (using new Area( line2d ) );
take the first area and call intersect with the second area obtained from the line.
your first area is now the intersection of the first.
take your leftmost, topmost, bottomost, rightmost coordinates
turn them into a line2d
and here you are.
Option 1, approximate the Line2D by triangles:
Polygon polygon = new Polygon(new int[]{x1, x2, x2}, new int[]{y1, y2+e, y2-e}, 3);
Area triangle = new Area(polygon);
triangle.intersect(area); // intersection of ray and area
return !triangle.isEmpty(); // returns true if intersects
Option 2, use the Geometry class instead
private final GeometryFactory geometryFactory = new GeometryFactory();
private ShapeReader shapeReader = new ShapeReader(geometryFactory);
Path2D.Double thePath = new Path2D.Double(area);
Geometry geometry = shapeReader.read(thePath.getPathIterator(null));
Coordinate[] coordinate = new Coordinate[] {new Coordinate(x1, y1), new Coordinate(x2, y2)};
LineString centerRay = geometryFactory.createLineString(coordinate);
return geometry.intersects(centerRay); // returns true if intersects