Openlayers - Get shortest line between two geometries












0














Using OL4 and Angular 5, I want to calculate the shortest line between two geometries, because I want to draw the shortest line between two geometries.



for example I have two polygons like this:



const geom1 = new ol_geom_Polygon([[[39.08317178, 34.94428969], [40.15753633, 35.19891679], 
[40.09419625, 35.46617166], [39.0198317, 35.21154456], [39.08317178, 34.94428969]]]);

const geom2 = new ol_geom_Polygon([[[42.06884752, 37.70855705], [41.28393081, 37.41465862],
[41.93091268, 36.88185002], [42.06884752, 37.70855705]]]);


OL4 geometry class has getClosestPoint function but it return the closest point of the geometry to a passed point.



I need a similar function but it receive a geometry object instead of point.



Regards.










share|improve this question





























    0














    Using OL4 and Angular 5, I want to calculate the shortest line between two geometries, because I want to draw the shortest line between two geometries.



    for example I have two polygons like this:



    const geom1 = new ol_geom_Polygon([[[39.08317178, 34.94428969], [40.15753633, 35.19891679], 
    [40.09419625, 35.46617166], [39.0198317, 35.21154456], [39.08317178, 34.94428969]]]);

    const geom2 = new ol_geom_Polygon([[[42.06884752, 37.70855705], [41.28393081, 37.41465862],
    [41.93091268, 36.88185002], [42.06884752, 37.70855705]]]);


    OL4 geometry class has getClosestPoint function but it return the closest point of the geometry to a passed point.



    I need a similar function but it receive a geometry object instead of point.



    Regards.










    share|improve this question



























      0












      0








      0







      Using OL4 and Angular 5, I want to calculate the shortest line between two geometries, because I want to draw the shortest line between two geometries.



      for example I have two polygons like this:



      const geom1 = new ol_geom_Polygon([[[39.08317178, 34.94428969], [40.15753633, 35.19891679], 
      [40.09419625, 35.46617166], [39.0198317, 35.21154456], [39.08317178, 34.94428969]]]);

      const geom2 = new ol_geom_Polygon([[[42.06884752, 37.70855705], [41.28393081, 37.41465862],
      [41.93091268, 36.88185002], [42.06884752, 37.70855705]]]);


      OL4 geometry class has getClosestPoint function but it return the closest point of the geometry to a passed point.



      I need a similar function but it receive a geometry object instead of point.



      Regards.










      share|improve this question















      Using OL4 and Angular 5, I want to calculate the shortest line between two geometries, because I want to draw the shortest line between two geometries.



      for example I have two polygons like this:



      const geom1 = new ol_geom_Polygon([[[39.08317178, 34.94428969], [40.15753633, 35.19891679], 
      [40.09419625, 35.46617166], [39.0198317, 35.21154456], [39.08317178, 34.94428969]]]);

      const geom2 = new ol_geom_Polygon([[[42.06884752, 37.70855705], [41.28393081, 37.41465862],
      [41.93091268, 36.88185002], [42.06884752, 37.70855705]]]);


      OL4 geometry class has getClosestPoint function but it return the closest point of the geometry to a passed point.



      I need a similar function but it receive a geometry object instead of point.



      Regards.







      openlayers






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 8:46







      abd0991

















      asked Nov 21 '18 at 15:01









      abd0991abd0991

      1417




      1417
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I think any shortest line will always include one of the vertices of one of the linestrings (for polygons the linestrings will be the outer rings), therefore whichever of those coordinates gives the closest closest point is the shortest line. I've checked the source code for getClosestPoint and it uses a simple pythagoras distance calculation, so geometries should be transformed to map coordinates before calling a function such as this which returns a line from geom1 to geom2 where the geometries can be single polygons and/or single linestrings.



          function getShortestLine(geom1, geom2) {

          var result;
          var distanceSq = Infinity;

          (geom1.getLinearRing ? geom1.getLinearRing(0) : geom1).getCoordinates().forEach(function(coordinates) {
          var closest = geom2.getClosestPoint(coordinates);
          var distanceNew = Math.pow(closest[0]-coordinates[0],2) + Math.pow(closest[1]-coordinates[1],2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [ coordinates, closest ];
          }
          });

          (geom2.getLinearRing ? geom2.getLinearRing(0) : geom2).getCoordinates().forEach(function(coordinates) {
          var closest = geom1.getClosestPoint(coordinates);
          var distanceNew = Math.pow(closest[0]-coordinates[0],2) + Math.pow(closest[1]-coordinates[1],2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [ closest, coordinates ];
          }
          });

          return new ol.geom.LineString(result);

          }





          share|improve this answer





















          • Great! I tested it and it works perfectly. Thx bro.
            – abd0991
            Nov 24 '18 at 10:27





















          0














          Actually, Mike's answer is about 95% perfect :) .. so I done some improvements and here is the final code in TypeScript:



            public getShortestLine(geom1: ol_geom_Geometry, geom2: ol_geom_Geometry): ol_geom_LineString {
          if (geom1.getType() === 'Point' && geom2.getType() === 'Point') {
          return new ol_geom_LineString([(geom1 as ol_geom_Point).getCoordinates(), (geom2 as ol_geom_Point).getCoordinates()]);
          }

          let result = ;
          let distanceSq = Infinity;

          let geom1Coordinates = ;
          if (geom1.getType() === 'Polygon') {
          geom1Coordinates = (geom1 as ol_geom_Polygon).getLinearRing(0).getCoordinates();
          } else if (geom1.getType() === 'Point') {
          geom1Coordinates = [(geom1 as ol_geom_Point).getCoordinates()];
          } else {
          geom1Coordinates = (geom1 as any).getCoordinates();
          }

          let geom2Coordinates = ;
          if (geom2.getType() === 'Polygon') {
          geom2Coordinates = (geom2 as ol_geom_Polygon).getLinearRing(0).getCoordinates();
          } else if (geom2.getType() === 'Point') {
          geom2Coordinates = [(geom2 as ol_geom_Point).getCoordinates()];
          } else {
          geom2Coordinates = (geom2 as any).getCoordinates();
          }

          geom1Coordinates.forEach(coordinates => {
          const closest = geom2.getClosestPoint(coordinates);
          const distanceNew = Math.pow(closest[0] - coordinates[0], 2) + Math.pow(closest[1] - coordinates[1], 2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [coordinates, closest];
          }
          });

          geom2Coordinates.forEach(coordinates => {
          const closest = geom1.getClosestPoint(coordinates);
          const distanceNew = Math.pow(closest[0] - coordinates[0], 2) + Math.pow(closest[1] - coordinates[1], 2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [closest, coordinates];
          }
          });

          return new ol_geom_LineString(result);
          }





          share|improve this answer





















          • I didn't bother with Point as there was already a method, but it's good to have it included in a single function. Circle also needs a special case, but it's easy. Get the closest point on the other geometry from the circle center, and from that get the closest point on the circle circumference.
            – Mike
            Nov 25 '18 at 17:29










          • Because Circle geometry is not supported by GeoJSON, I always convert it to a Polygon geometry so there is no problem. Thanks again I appreciate your helps.
            – abd0991
            Nov 26 '18 at 10:48













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414852%2fopenlayers-get-shortest-line-between-two-geometries%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I think any shortest line will always include one of the vertices of one of the linestrings (for polygons the linestrings will be the outer rings), therefore whichever of those coordinates gives the closest closest point is the shortest line. I've checked the source code for getClosestPoint and it uses a simple pythagoras distance calculation, so geometries should be transformed to map coordinates before calling a function such as this which returns a line from geom1 to geom2 where the geometries can be single polygons and/or single linestrings.



          function getShortestLine(geom1, geom2) {

          var result;
          var distanceSq = Infinity;

          (geom1.getLinearRing ? geom1.getLinearRing(0) : geom1).getCoordinates().forEach(function(coordinates) {
          var closest = geom2.getClosestPoint(coordinates);
          var distanceNew = Math.pow(closest[0]-coordinates[0],2) + Math.pow(closest[1]-coordinates[1],2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [ coordinates, closest ];
          }
          });

          (geom2.getLinearRing ? geom2.getLinearRing(0) : geom2).getCoordinates().forEach(function(coordinates) {
          var closest = geom1.getClosestPoint(coordinates);
          var distanceNew = Math.pow(closest[0]-coordinates[0],2) + Math.pow(closest[1]-coordinates[1],2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [ closest, coordinates ];
          }
          });

          return new ol.geom.LineString(result);

          }





          share|improve this answer





















          • Great! I tested it and it works perfectly. Thx bro.
            – abd0991
            Nov 24 '18 at 10:27


















          0














          I think any shortest line will always include one of the vertices of one of the linestrings (for polygons the linestrings will be the outer rings), therefore whichever of those coordinates gives the closest closest point is the shortest line. I've checked the source code for getClosestPoint and it uses a simple pythagoras distance calculation, so geometries should be transformed to map coordinates before calling a function such as this which returns a line from geom1 to geom2 where the geometries can be single polygons and/or single linestrings.



          function getShortestLine(geom1, geom2) {

          var result;
          var distanceSq = Infinity;

          (geom1.getLinearRing ? geom1.getLinearRing(0) : geom1).getCoordinates().forEach(function(coordinates) {
          var closest = geom2.getClosestPoint(coordinates);
          var distanceNew = Math.pow(closest[0]-coordinates[0],2) + Math.pow(closest[1]-coordinates[1],2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [ coordinates, closest ];
          }
          });

          (geom2.getLinearRing ? geom2.getLinearRing(0) : geom2).getCoordinates().forEach(function(coordinates) {
          var closest = geom1.getClosestPoint(coordinates);
          var distanceNew = Math.pow(closest[0]-coordinates[0],2) + Math.pow(closest[1]-coordinates[1],2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [ closest, coordinates ];
          }
          });

          return new ol.geom.LineString(result);

          }





          share|improve this answer





















          • Great! I tested it and it works perfectly. Thx bro.
            – abd0991
            Nov 24 '18 at 10:27
















          0












          0








          0






          I think any shortest line will always include one of the vertices of one of the linestrings (for polygons the linestrings will be the outer rings), therefore whichever of those coordinates gives the closest closest point is the shortest line. I've checked the source code for getClosestPoint and it uses a simple pythagoras distance calculation, so geometries should be transformed to map coordinates before calling a function such as this which returns a line from geom1 to geom2 where the geometries can be single polygons and/or single linestrings.



          function getShortestLine(geom1, geom2) {

          var result;
          var distanceSq = Infinity;

          (geom1.getLinearRing ? geom1.getLinearRing(0) : geom1).getCoordinates().forEach(function(coordinates) {
          var closest = geom2.getClosestPoint(coordinates);
          var distanceNew = Math.pow(closest[0]-coordinates[0],2) + Math.pow(closest[1]-coordinates[1],2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [ coordinates, closest ];
          }
          });

          (geom2.getLinearRing ? geom2.getLinearRing(0) : geom2).getCoordinates().forEach(function(coordinates) {
          var closest = geom1.getClosestPoint(coordinates);
          var distanceNew = Math.pow(closest[0]-coordinates[0],2) + Math.pow(closest[1]-coordinates[1],2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [ closest, coordinates ];
          }
          });

          return new ol.geom.LineString(result);

          }





          share|improve this answer












          I think any shortest line will always include one of the vertices of one of the linestrings (for polygons the linestrings will be the outer rings), therefore whichever of those coordinates gives the closest closest point is the shortest line. I've checked the source code for getClosestPoint and it uses a simple pythagoras distance calculation, so geometries should be transformed to map coordinates before calling a function such as this which returns a line from geom1 to geom2 where the geometries can be single polygons and/or single linestrings.



          function getShortestLine(geom1, geom2) {

          var result;
          var distanceSq = Infinity;

          (geom1.getLinearRing ? geom1.getLinearRing(0) : geom1).getCoordinates().forEach(function(coordinates) {
          var closest = geom2.getClosestPoint(coordinates);
          var distanceNew = Math.pow(closest[0]-coordinates[0],2) + Math.pow(closest[1]-coordinates[1],2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [ coordinates, closest ];
          }
          });

          (geom2.getLinearRing ? geom2.getLinearRing(0) : geom2).getCoordinates().forEach(function(coordinates) {
          var closest = geom1.getClosestPoint(coordinates);
          var distanceNew = Math.pow(closest[0]-coordinates[0],2) + Math.pow(closest[1]-coordinates[1],2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [ closest, coordinates ];
          }
          });

          return new ol.geom.LineString(result);

          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 14:51









          MikeMike

          1,19627




          1,19627












          • Great! I tested it and it works perfectly. Thx bro.
            – abd0991
            Nov 24 '18 at 10:27




















          • Great! I tested it and it works perfectly. Thx bro.
            – abd0991
            Nov 24 '18 at 10:27


















          Great! I tested it and it works perfectly. Thx bro.
          – abd0991
          Nov 24 '18 at 10:27






          Great! I tested it and it works perfectly. Thx bro.
          – abd0991
          Nov 24 '18 at 10:27















          0














          Actually, Mike's answer is about 95% perfect :) .. so I done some improvements and here is the final code in TypeScript:



            public getShortestLine(geom1: ol_geom_Geometry, geom2: ol_geom_Geometry): ol_geom_LineString {
          if (geom1.getType() === 'Point' && geom2.getType() === 'Point') {
          return new ol_geom_LineString([(geom1 as ol_geom_Point).getCoordinates(), (geom2 as ol_geom_Point).getCoordinates()]);
          }

          let result = ;
          let distanceSq = Infinity;

          let geom1Coordinates = ;
          if (geom1.getType() === 'Polygon') {
          geom1Coordinates = (geom1 as ol_geom_Polygon).getLinearRing(0).getCoordinates();
          } else if (geom1.getType() === 'Point') {
          geom1Coordinates = [(geom1 as ol_geom_Point).getCoordinates()];
          } else {
          geom1Coordinates = (geom1 as any).getCoordinates();
          }

          let geom2Coordinates = ;
          if (geom2.getType() === 'Polygon') {
          geom2Coordinates = (geom2 as ol_geom_Polygon).getLinearRing(0).getCoordinates();
          } else if (geom2.getType() === 'Point') {
          geom2Coordinates = [(geom2 as ol_geom_Point).getCoordinates()];
          } else {
          geom2Coordinates = (geom2 as any).getCoordinates();
          }

          geom1Coordinates.forEach(coordinates => {
          const closest = geom2.getClosestPoint(coordinates);
          const distanceNew = Math.pow(closest[0] - coordinates[0], 2) + Math.pow(closest[1] - coordinates[1], 2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [coordinates, closest];
          }
          });

          geom2Coordinates.forEach(coordinates => {
          const closest = geom1.getClosestPoint(coordinates);
          const distanceNew = Math.pow(closest[0] - coordinates[0], 2) + Math.pow(closest[1] - coordinates[1], 2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [closest, coordinates];
          }
          });

          return new ol_geom_LineString(result);
          }





          share|improve this answer





















          • I didn't bother with Point as there was already a method, but it's good to have it included in a single function. Circle also needs a special case, but it's easy. Get the closest point on the other geometry from the circle center, and from that get the closest point on the circle circumference.
            – Mike
            Nov 25 '18 at 17:29










          • Because Circle geometry is not supported by GeoJSON, I always convert it to a Polygon geometry so there is no problem. Thanks again I appreciate your helps.
            – abd0991
            Nov 26 '18 at 10:48


















          0














          Actually, Mike's answer is about 95% perfect :) .. so I done some improvements and here is the final code in TypeScript:



            public getShortestLine(geom1: ol_geom_Geometry, geom2: ol_geom_Geometry): ol_geom_LineString {
          if (geom1.getType() === 'Point' && geom2.getType() === 'Point') {
          return new ol_geom_LineString([(geom1 as ol_geom_Point).getCoordinates(), (geom2 as ol_geom_Point).getCoordinates()]);
          }

          let result = ;
          let distanceSq = Infinity;

          let geom1Coordinates = ;
          if (geom1.getType() === 'Polygon') {
          geom1Coordinates = (geom1 as ol_geom_Polygon).getLinearRing(0).getCoordinates();
          } else if (geom1.getType() === 'Point') {
          geom1Coordinates = [(geom1 as ol_geom_Point).getCoordinates()];
          } else {
          geom1Coordinates = (geom1 as any).getCoordinates();
          }

          let geom2Coordinates = ;
          if (geom2.getType() === 'Polygon') {
          geom2Coordinates = (geom2 as ol_geom_Polygon).getLinearRing(0).getCoordinates();
          } else if (geom2.getType() === 'Point') {
          geom2Coordinates = [(geom2 as ol_geom_Point).getCoordinates()];
          } else {
          geom2Coordinates = (geom2 as any).getCoordinates();
          }

          geom1Coordinates.forEach(coordinates => {
          const closest = geom2.getClosestPoint(coordinates);
          const distanceNew = Math.pow(closest[0] - coordinates[0], 2) + Math.pow(closest[1] - coordinates[1], 2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [coordinates, closest];
          }
          });

          geom2Coordinates.forEach(coordinates => {
          const closest = geom1.getClosestPoint(coordinates);
          const distanceNew = Math.pow(closest[0] - coordinates[0], 2) + Math.pow(closest[1] - coordinates[1], 2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [closest, coordinates];
          }
          });

          return new ol_geom_LineString(result);
          }





          share|improve this answer





















          • I didn't bother with Point as there was already a method, but it's good to have it included in a single function. Circle also needs a special case, but it's easy. Get the closest point on the other geometry from the circle center, and from that get the closest point on the circle circumference.
            – Mike
            Nov 25 '18 at 17:29










          • Because Circle geometry is not supported by GeoJSON, I always convert it to a Polygon geometry so there is no problem. Thanks again I appreciate your helps.
            – abd0991
            Nov 26 '18 at 10:48
















          0












          0








          0






          Actually, Mike's answer is about 95% perfect :) .. so I done some improvements and here is the final code in TypeScript:



            public getShortestLine(geom1: ol_geom_Geometry, geom2: ol_geom_Geometry): ol_geom_LineString {
          if (geom1.getType() === 'Point' && geom2.getType() === 'Point') {
          return new ol_geom_LineString([(geom1 as ol_geom_Point).getCoordinates(), (geom2 as ol_geom_Point).getCoordinates()]);
          }

          let result = ;
          let distanceSq = Infinity;

          let geom1Coordinates = ;
          if (geom1.getType() === 'Polygon') {
          geom1Coordinates = (geom1 as ol_geom_Polygon).getLinearRing(0).getCoordinates();
          } else if (geom1.getType() === 'Point') {
          geom1Coordinates = [(geom1 as ol_geom_Point).getCoordinates()];
          } else {
          geom1Coordinates = (geom1 as any).getCoordinates();
          }

          let geom2Coordinates = ;
          if (geom2.getType() === 'Polygon') {
          geom2Coordinates = (geom2 as ol_geom_Polygon).getLinearRing(0).getCoordinates();
          } else if (geom2.getType() === 'Point') {
          geom2Coordinates = [(geom2 as ol_geom_Point).getCoordinates()];
          } else {
          geom2Coordinates = (geom2 as any).getCoordinates();
          }

          geom1Coordinates.forEach(coordinates => {
          const closest = geom2.getClosestPoint(coordinates);
          const distanceNew = Math.pow(closest[0] - coordinates[0], 2) + Math.pow(closest[1] - coordinates[1], 2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [coordinates, closest];
          }
          });

          geom2Coordinates.forEach(coordinates => {
          const closest = geom1.getClosestPoint(coordinates);
          const distanceNew = Math.pow(closest[0] - coordinates[0], 2) + Math.pow(closest[1] - coordinates[1], 2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [closest, coordinates];
          }
          });

          return new ol_geom_LineString(result);
          }





          share|improve this answer












          Actually, Mike's answer is about 95% perfect :) .. so I done some improvements and here is the final code in TypeScript:



            public getShortestLine(geom1: ol_geom_Geometry, geom2: ol_geom_Geometry): ol_geom_LineString {
          if (geom1.getType() === 'Point' && geom2.getType() === 'Point') {
          return new ol_geom_LineString([(geom1 as ol_geom_Point).getCoordinates(), (geom2 as ol_geom_Point).getCoordinates()]);
          }

          let result = ;
          let distanceSq = Infinity;

          let geom1Coordinates = ;
          if (geom1.getType() === 'Polygon') {
          geom1Coordinates = (geom1 as ol_geom_Polygon).getLinearRing(0).getCoordinates();
          } else if (geom1.getType() === 'Point') {
          geom1Coordinates = [(geom1 as ol_geom_Point).getCoordinates()];
          } else {
          geom1Coordinates = (geom1 as any).getCoordinates();
          }

          let geom2Coordinates = ;
          if (geom2.getType() === 'Polygon') {
          geom2Coordinates = (geom2 as ol_geom_Polygon).getLinearRing(0).getCoordinates();
          } else if (geom2.getType() === 'Point') {
          geom2Coordinates = [(geom2 as ol_geom_Point).getCoordinates()];
          } else {
          geom2Coordinates = (geom2 as any).getCoordinates();
          }

          geom1Coordinates.forEach(coordinates => {
          const closest = geom2.getClosestPoint(coordinates);
          const distanceNew = Math.pow(closest[0] - coordinates[0], 2) + Math.pow(closest[1] - coordinates[1], 2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [coordinates, closest];
          }
          });

          geom2Coordinates.forEach(coordinates => {
          const closest = geom1.getClosestPoint(coordinates);
          const distanceNew = Math.pow(closest[0] - coordinates[0], 2) + Math.pow(closest[1] - coordinates[1], 2);
          if (distanceNew < distanceSq) {
          distanceSq = distanceNew;
          result = [closest, coordinates];
          }
          });

          return new ol_geom_LineString(result);
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 17:08









          abd0991abd0991

          1417




          1417












          • I didn't bother with Point as there was already a method, but it's good to have it included in a single function. Circle also needs a special case, but it's easy. Get the closest point on the other geometry from the circle center, and from that get the closest point on the circle circumference.
            – Mike
            Nov 25 '18 at 17:29










          • Because Circle geometry is not supported by GeoJSON, I always convert it to a Polygon geometry so there is no problem. Thanks again I appreciate your helps.
            – abd0991
            Nov 26 '18 at 10:48




















          • I didn't bother with Point as there was already a method, but it's good to have it included in a single function. Circle also needs a special case, but it's easy. Get the closest point on the other geometry from the circle center, and from that get the closest point on the circle circumference.
            – Mike
            Nov 25 '18 at 17:29










          • Because Circle geometry is not supported by GeoJSON, I always convert it to a Polygon geometry so there is no problem. Thanks again I appreciate your helps.
            – abd0991
            Nov 26 '18 at 10:48


















          I didn't bother with Point as there was already a method, but it's good to have it included in a single function. Circle also needs a special case, but it's easy. Get the closest point on the other geometry from the circle center, and from that get the closest point on the circle circumference.
          – Mike
          Nov 25 '18 at 17:29




          I didn't bother with Point as there was already a method, but it's good to have it included in a single function. Circle also needs a special case, but it's easy. Get the closest point on the other geometry from the circle center, and from that get the closest point on the circle circumference.
          – Mike
          Nov 25 '18 at 17:29












          Because Circle geometry is not supported by GeoJSON, I always convert it to a Polygon geometry so there is no problem. Thanks again I appreciate your helps.
          – abd0991
          Nov 26 '18 at 10:48






          Because Circle geometry is not supported by GeoJSON, I always convert it to a Polygon geometry so there is no problem. Thanks again I appreciate your helps.
          – abd0991
          Nov 26 '18 at 10:48




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414852%2fopenlayers-get-shortest-line-between-two-geometries%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen