0

I'm working on a road network program, and want to display the road network as a quick visual to verify it's set up properly.

I have the coordinates of each junction, and want to convert them to one's I can use in JavaFX, eg: Canvas.

However, the coordinates for longitude are in the negatives, and I'm stuck on converting them to, say, a window of 750 x 750

I have the min & max latitude and longitude values

lowest longitude value: -122.5142235 highest longitude value: -122.3637617

lowest latitude value: 37.7082614 highest latitude value: 37.8174339

so I need to figure out how to scale the distance between each junction while maintining position and distance between the junctions, and transform it to make it able to appear on the canvas, as currently it starts at -122

Thanks in advance for any help.

I've tried using different methods such as this:

GraphicsContext gc = canvas.getGraphicsContext2D();

Double maxX = -122.3637617;
Double minX = -122.5142235;

Double maxY = 37.8174339;
Double minY = 37.7082614;

int width = 750;
int height = 750;

double xrange = maxX - minX;
double yrange = maxY - minY;

double xratio = xrange / width;
double yratio = yrange / height;

// width and height are in units of pixels
// gc is the GraphicsContext object from javafx
Affine t = new Affine();

t.appendTranslation( minX, minY );
t.appendScale( (xratio*150),(yratio*150) );
try
{
    t.invert();
}
catch(NonInvertibleTransformException e )
{
    throw new RuntimeException( e.getMessage(), e );
}
gc.setTransform( t );

gc.setFill(Color.BLACK);
gc.setLineWidth(1);
gc.strokeLine(-122.40351, 37.80489, -122.40345, 37.80504);

but it doesn't seem to work properly.

I've also tried this code:

Double x1 = xConversion(maxX, minX, -122.40351, width);
Double x2 = xConversion(maxX, minX, -122.40345, width);

Double y1 = yConversion(maxY, minY, 37.80489, height);
Double y2 = yConversion(maxY, minY, 37.80504, height);

Scale scale = new Scale();
scale.setX(xrange*width);
scale.setY(yrange*height);

Line line = new Line(x1, y1, x2, y2);

line.getTransforms().add(scale);


public Double xConversion(Double maxX, Double minX, Double x, int width)
{
    //return (minX)+Math.abs((maxX)-(minX))*(x+0.5)/width;
    return x-minX;
}

public Double yConversion(Double maxY, Double minY, Double y, int height)
{
    return y-maxY;
    //return (minY)+Math.abs((maxY)-(minY))*(y+0.5)/height;
}

but the best result I've got has been a square appearing, instead of a line, which is a bit wrong XD.

m19v
  • 1,800
  • 4
  • 11
  • 26
S1ft
  • 3
  • 1
  • 1
    I think you are actually trying to convert coordinates to a canvas and not a graph. An example for a graph is the transforms in the [`mapX` and `mapY` functions in this example](https://stackoverflow.com/questions/24005247/draw-cartesian-plane-graph-with-canvas-in-javafx/24008426#24008426). Your calculation won't be identical but will be similar. You calculate a translate factor and a scale factor based on the extents of each co-ordinate system (the canvas and the model), then apply them to each model coordinate to get a canvas coordinate. – jewelsea Nov 06 '22 at 17:15
  • 1
    Also see the [window to viewport mapping section of this lecture](http://web.cs.wpi.edu/~emmanuel/courses/cs543/slides/lecture02_p1.pdf) for the general formulas involved. – jewelsea Nov 06 '22 at 17:29
  • The approach is also outlined [here](https://stackoverflow.com/a/42771430/230513) and [here](https://stackoverflow.com/a/32171751/230513). – trashgod Nov 06 '22 at 18:36
  • And don't use "Double" when you are actually just dealing with values. Use "double" instead. – mipa Nov 07 '22 at 10:09
  • Also, if this is a serious project you should use some kind of map projection. Have a look here for example: https://github.com/geographiclib/geographiclib-java – mipa Nov 07 '22 at 10:16
  • Thank you for all the suggestions! Mostly the visualisation of it isn't relevant to the project - it's just for me to sanity check I've build it properly in terms of the roads connecting properly / to help me visualise it to look for issues with the algorithm – S1ft Nov 07 '22 at 10:35

0 Answers0