d3 / d3-geo

Geographic projections, spherical shapes and spherical trigonometry.

Home Page:https://d3js.org/d3-geo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] API for chekcing if point is in view or not given projection

NickGeneva opened this issue · comments

Hi!

I'm working with D3 geo and was doing some manual plotting of points on a certain projection. I can use the projection object to convert a lat/long coordinate to a pixel location on a canvas. This works fine however, this projection is rotating, so this point may be on the front of the globe or backside.

My question is if there's some built in API I can call to tell me if a given point is in view of the current canvas / context? How is this done? I'm assuming this process typically occurs somewhere when calling the geoPath to plot, but I need to plot these points manually. Thank you!

// Spin globe
for(var i = 0; i < 360; i+=1){
      let projection = d3.geoOrthographic()
        .scale(250)
        .center([0, 0])
        .rotate([i,-30])
      // Get point
     point1 = projection(point);
     // Is the point on front/backside of the globe?
     if(point is infront of globe){
        // Draw point on canvas
    }
}

Hi @mbostock
Great, this is very helpful. Thanks!

Follow up: Are there any other example(s) I can look at to learn more about these projection streams and modify them? I'm not able to fully understand them just from the API docs and am trying to get a little better understanding of what is going on here under the hood.

Thanks!

Not sure where there are examples in the traditional sense, but one place where we use them in a relatively readable way is in Plot's projection source, starting on line 77: the stream is computed as
{stream: (s) => projection.stream(transform.stream(clip(s)))}

which you can read as: "project, then apply the (linear) transform (scale + translate), then clip"