How to use @conveyal/lonlat - 10 common examples

To help you get started, we’ve selected a few @conveyal/lonlat examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github conveyal / taui / src / actions / config.js View on Github external
...request,
          ready: true,
          transitive
        }
      })

      // Set all the networks
      fullNetworks.forEach(n => dispatch(setNetwork(n)))

      try {
        // Load the config start coordinate or the center
        const centerFromMap = get(map, 'centerCoordinates', getCenterFromNetwork(fullNetworks[0]))
        const centerCoordinates = startCoordinate || centerFromMap

        const geocoder = {
          proximity: lonlat.toString(centerFromMap)
        }
        const maxBounds = get(map, 'maxBounds')
        if (maxBounds) {
          geocoder.bbox = typeof maxBounds === 'string'
            ? maxBounds
            : `${maxBounds[0].join(',')},${maxBounds[1].join(',')}`
        }

        dispatch({type: 'set geocoder', payload: geocoder})
        dispatch(updateStartPosition(centerCoordinates))
      } catch (e) {
        console.error(e)
      }
    }
  }))
github conveyal / analysis-ui / lib / components / map / regional.js View on Github external
onMapClick = (e: MouseEvent) => {
    const {
      _id,
      comparisonId,
      setRegionalAnalysisOrigin,
      isRegionalAnalysisSamplingDistributionComponentOnMap
    } = this.props
    // if the destination component is already on the map, don't add it - change
    // destination by dragging destination marker
    if (!isRegionalAnalysisSamplingDistributionComponentOnMap) {
      setRegionalAnalysisOrigin({
        regionalAnalysisId: _id,
        comparisonRegionalAnalysisId: comparisonId,
        lonlat: lonlat(e.latlng)
      })
    }
  }
github conveyal / analysis-ui / lib / components / modifications-map / transit-editor / index.js View on Github external
_dragStop = index => event => {
    logDomEvent('_dragStop', event)
    Leaflet.DomEvent.stop(event)
    const {followRoad, updateModification} = this.props
    const segments = this._getSegments()
    const position = event.target.getLatLng()
    const snapStop = this._getStopNear(position)
    const isEnd = index === segments.length
    const isStart = index === 0

    let coordinates = lonlat.toCoordinates(position)
    let newStopId
    if (snapStop) {
      newStopId = snapStop.stop_id
      coordinates = [snapStop.stop_lon, snapStop.stop_lat]
    }

    runAsync(async () => {
      const newSegments = [...segments]
      if (!isStart) {
        const previousSegment = segments[index - 1]
        const geometry = await getLineString(
          coordinatesFromSegment(previousSegment),
          coordinates,
          {followRoad}
        )
        // will overwrite geometry and preserve other attributes
github conveyal / analysis-ui / lib / utils / get-stop-near-point.js View on Github external
export default function getStopNearPoint (
  point: LonLatC,
  allStops: GTFSStop[],
  zoom: number
): GTFSStop | void {
  // base snap distance on map zoom
  const metersPerPixel = CIRCUMFERENCE_OF_EARTH_METERS / (256 * Math.pow(2, zoom))
  const maxDistanceKilometers = (PIXEL_RADIUS * metersPerPixel) / 1000
  const clickPoint = turf.point(lonlat.toCoordinates(point))

  let closestStopDistance = Infinity
  let closestStop
  for (const stop of allStops) {
    const stopDistance = turfDistance(clickPoint, turf.point([stop.stop_lon, stop.stop_lat]))
    if (stopDistance < maxDistanceKilometers && stopDistance < closestStopDistance) {
      closestStopDistance = stopDistance
      closestStop = stop
    }
  }

  return closestStop
}
github ibi-group / datatools-ui / lib / editor / actions / map / stopStrategies.js View on Github external
return async function (dispatch: dispatchFn, getState: getStateFn) {
    const {followStreets} = getState().editor.editSettings.present
    const {controlPoints, patternSegments} = getControlPoints(getState())
    const clonedControlPoints = clone(controlPoints)
    let newShape
    if (followStreets) {
      newShape = await getPolyline([endPoint, newEndPoint])
    }
    if (!newShape) {
      // Get single coordinate for straight line if polyline fails or if not
      // following streets.
      newShape = [ll.toCoordinates(endPoint), ll.toCoordinates(newEndPoint)]
    }
    const initialDistance = pattern.shape
      ? lineDistance(pattern.shape, 'meters')
      : 0
    const newLineSegment = lineString(newShape)
    const distanceAdded = lineDistance(newLineSegment, 'meters')
    const newPatternSegments = [...patternSegments]
    if (splitInterval > 0) {
      // If split interval is provided (e.g., to add stops at intervals along
      // new segment), split new line segment and add temp control points (later
      // to be assigned stop IDs).
      const numIntervals = Math.floor(distanceAdded / splitInterval)
      let previousDistance = 0
      // Iterate over intervals and store positions for constructing stops
      for (let i = 1; i <= numIntervals; i++) {
        const splitDistance = (i * splitInterval)
github ibi-group / datatools-ui / lib / editor / util / map.js View on Github external
if (editType === 'update') {
    // TODO: Sometimes the starting or ending control point can go off the
    // street network, but it's probably not worth slicing the coordinates like
    // the below commented out code. If there is a reliable way of trimming the
    // GraphHopper response to exclude non-network paths, we should use that.
    // if (index === 0) {
    //   // Remove first coordinate if routing to the first control point (first
    //   // pattern stop) because this is likely off the street grid.
    //   newSegment.coordinates.splice(0, 1)
    // } else if (index === controlPoints.length - 1) {
    //   // Remove last coordinate if routing to the last control point (last
    //   // pattern stop) because this is likely off the street grid.
    //   newSegment.coordinates.splice(-1, 1)
    // }
    // Add control point location to snap to line (if needed)
    const controlPointGeoJson = point(ll.toCoordinates(newPoint))
    const {
      snapPoint,
      distTraveled,
      index: snapIndex,
      beforeSlice,
      afterSlice
    } = getControlPointSnap(
      controlPointGeoJson,
      ensureValidCoords(newSegment.coordinates)
    )
    // Update surrounding pattern coordinate segments regardless of whether snap
    // is set to true
    if (index > 0) {
      // Only update previous segment if index is greater than zero
      updatedPatternCoordinates[index - 1] = beforeSlice.geometry.coordinates
    }
github ibi-group / datatools-ui / lib / editor / actions / map / index.js View on Github external
return function (dispatch, getState) {
    // slice line
    const beginPoint = point(pattern.shape.coordinates[0])
    const coord = ll.toCoordinates(latlng)
    const clickPoint = point(coord)
    const lineSegment = lineSlice(beginPoint, clickPoint, pattern.shape)

    // measure line segment
    const distTraveled = lineDistance(lineSegment, 'meters')
    const controlPoint = newControlPoint(distTraveled, clickPoint)

    // find splice index based on shape dist traveled
    let index = 0
    for (var i = 0; i < controlPoints.length; i++) {
      if (distTraveled > controlPoints[i].distance) {
        index = i + 1
      } else {
        break
      }
    }
github conveyal / analysis-ui / lib / components / modifications-map / transit-editor / index.js View on Github external
_dragControlPoint = index => event => {
    logDomEvent('_dragControlPoint', event)
    Leaflet.DomEvent.stop(event)
    const {followRoad, updateModification} = this.props
    const coordinates = lonlat.toCoordinates(event.target.getLatLng())
    const segments = this._getSegments()
    const isEnd = index === segments.length
    const isStart = index === 0

    runAsync(async () => {
      const newSegments = [...segments]
      if (!isStart) {
        const previousSegment = newSegments[index - 1]
        // will overwrite geometry and preserve other attributes
        newSegments[index - 1] = {
          ...previousSegment,
          geometry: await getLineString(
            coordinatesFromSegment(previousSegment),
            coordinates,
            {followRoad}
          )
github conveyal / analysis-ui / lib / components / map / draggable-popup.js View on Github external
render () {
    // remove and drag aren't used here but they shouldn't be passed into the popup
    const {position, children, remove, drag, dragEnd, ...rest} = this.props

    return (
      <span>
        </span>
    )
  }
github ibi-group / datatools-ui / lib / editor / actions / map / stopStrategies.js View on Github external
.then(result =&gt; {
          const newStopLatLngs = []
          // Iterate over newly added controlPoints and create stops for each.
          for (let i = controlPoints.length; i &lt; result.controlPoints.length; i++) {
            const controlPoint = result.controlPoints[i]
            const stopLatlng = ll.toLeaflet(controlPoint.point.geometry.coordinates)
            newStopLatLngs.push(stopLatlng)
          }
          // Create new stops at the interval points.
          return Promise.all(newStopLatLngs.map((latlng, i) =&gt; dispatch(
            addStopAtPoint(latlng, false, patternStops.length + i, activePattern))
          ))
            .then(newStops =&gt; {
              newStops.forEach((s, index) =&gt; {
                // Add new stop to pattern stops list
                if (s) {
                  const stopControlPoint = result.controlPoints[controlPoints.length + index]
                  const patternStop = stopToPatternStop(s)
                  // Set pattern stop's shape dist traveled.
                  patternStop.shapeDistTraveled = stopControlPoint.distance
                  patternStops.push(patternStop)
                  // Update stop properties on new control points.