How to use the @conveyal/lonlat.toCoordinates function in @conveyal/lonlat

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 / 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 / utils / get-line-string.js View on Github external
export default async function getLineString (from: LonLatC, to: LonLatC, {followRoad}: {
  followRoad: boolean
}) {
  try {
    if (followRoad) {
      const coordinates = await getRoutePolyline(from, to)
      const c0 = coordinates[0]
      const cy = coordinates[coordinates.length - 1]
      if (!coordinatesAreEqual(c0, from, MINIMUM_COORDINATE_DISTANCE)) {
        coordinates.unshift(lonlat.toCoordinates(from))
      }
      if (!coordinatesAreEqual(cy, to, MINIMUM_COORDINATE_DISTANCE)) {
        coordinates.push(lonlat.toCoordinates(to))
      }

      return {
        type: 'LineString',
        coordinates
      }
    } else {
      return await lineString([
        lonlat.toCoordinates(from),
        lonlat.toCoordinates(to)
      ]).geometry
    }
  } catch (e) {
    console.error(e.stack)
    throw e
  }
github conveyal / analysis-ui / lib / utils / get-line-string.js View on Github external
const cy = coordinates[coordinates.length - 1]
      if (!coordinatesAreEqual(c0, from, MINIMUM_COORDINATE_DISTANCE)) {
        coordinates.unshift(lonlat.toCoordinates(from))
      }
      if (!coordinatesAreEqual(cy, to, MINIMUM_COORDINATE_DISTANCE)) {
        coordinates.push(lonlat.toCoordinates(to))
      }

      return {
        type: 'LineString',
        coordinates
      }
    } else {
      return await lineString([
        lonlat.toCoordinates(from),
        lonlat.toCoordinates(to)
      ]).geometry
    }
  } catch (e) {
    console.error(e.stack)
    throw e
  }
}
github conveyal / analysis-ui / lib / utils / get-line-string.js View on Github external
export default async function getLineString (from: LonLatC, to: LonLatC, {followRoad}: {
  followRoad: boolean
}) {
  try {
    if (followRoad) {
      const coordinates = await getRoutePolyline(from, to)
      const c0 = coordinates[0]
      const cy = coordinates[coordinates.length - 1]
      if (!coordinatesAreEqual(c0, from, MINIMUM_COORDINATE_DISTANCE)) {
        coordinates.unshift(lonlat.toCoordinates(from))
      }
      if (!coordinatesAreEqual(cy, to, MINIMUM_COORDINATE_DISTANCE)) {
        coordinates.push(lonlat.toCoordinates(to))
      }

      return {
        type: 'LineString',
        coordinates
      }
    } else {
      return await lineString([
        lonlat.toCoordinates(from),
        lonlat.toCoordinates(to)
      ]).geometry
    }
  } catch (e) {