# 十一.geom(LineString)

# 1.LineString

查看代码详情
<template>
  <div>
    <div ref="map" class="map"></div>
    <form class="form-inline">
      <label for="type">几何类型 &nbsp;</label>
      <select ref="type">
        <option value="Polygon">多边形</option>
        <option value="LineString">直线</option>
        <option value="None"></option>
      </select>
    </form>
  </div>
</template>
  
  <script>
export default {
  mounted() {
    let {
      Feature,
      interaction: { Draw, Snap },
      geom: { LineString },
      Map,
      View,
      layer: { Tile: TileLayer, Vector: VectorLayer },
      source: { OSM, Vector: VectorSource },
      style: { Fill, Stroke, Style },
      format: { GeoJSON },
    } = ol;
    function length(a, b) {
      return Math.sqrt(
        (b[0] - a[0]) * (b[0] - a[0]) + (b[1] - a[1]) * (b[1] - a[1])
      );
    }
    function isOnSegment(c, a, b) {
      const lengthAc = length(a, c);
      const lengthAb = length(a, b);
      const dot =
        ((c[0] - a[0]) * (b[0] - a[0]) + (c[1] - a[1]) * (b[1] - a[1])) /
        lengthAb;
      return Math.abs(lengthAc - dot) < 1e-6 && lengthAc < lengthAb;
    }
    function mod(a, b) {
      return ((a % b) + b) % b;
    }
    function getPartialRingCoords(feature, startPoint, endPoint) {
      let polygon = feature.getGeometry();
      if (polygon.getType() === "MultiPolygon") {
        polygon = polygon.getPolygon(0);
      }
      const ringCoords = polygon.getLinearRing().getCoordinates();
      let i,
        pointA,
        pointB,
        startSegmentIndex = -1;
      for (i = 0; i < ringCoords.length; i++) {
        pointA = ringCoords[i];
        pointB = ringCoords[mod(i + 1, ringCoords.length)];
        if (isOnSegment(startPoint, pointA, pointB)) {
          startSegmentIndex = i;
          break;
        }
      }
      const cwCoordinates = [];
      let cwLength = 0;
      const ccwCoordinates = [];
      let ccwLength = 0;
      for (i = 0; i < ringCoords.length; i++) {
        pointA =
          i === 0
            ? startPoint
            : ringCoords[mod(i + startSegmentIndex, ringCoords.length)];
        pointB = ringCoords[mod(i + startSegmentIndex + 1, ringCoords.length)];
        cwCoordinates.push(pointA);
        if (isOnSegment(endPoint, pointA, pointB)) {
          cwCoordinates.push(endPoint);
          cwLength += length(pointA, endPoint);
          break;
        } else {
          cwLength += length(pointA, pointB);
        }
      }
      for (i = 0; i < ringCoords.length; i++) {
        pointA = ringCoords[mod(startSegmentIndex - i, ringCoords.length)];
        pointB =
          i === 0
            ? startPoint
            : ringCoords[mod(startSegmentIndex - i + 1, ringCoords.length)];
        ccwCoordinates.push(pointB);

        if (isOnSegment(endPoint, pointA, pointB)) {
          ccwCoordinates.push(endPoint);
          ccwLength += length(endPoint, pointB);
          break;
        } else {
          ccwLength += length(pointA, pointB);
        }
      }
      return ccwLength < cwLength ? ccwCoordinates : cwCoordinates;
    }
    const raster = new TileLayer({
      source: new OSM(),
    });
    const baseVector = new VectorLayer({
      source: new VectorSource({
        format: new GeoJSON(),
        url: "https://ahocevar.com/geoserver/wfs?service=wfs&request=getfeature&typename=topp:states&cql_filter=STATE_NAME='Idaho'&outputformat=application/json",
      }),
    });
    const drawVector = new VectorLayer({
      source: new VectorSource(),
      style: new Style({
        stroke: new Stroke({
          color: "rgba(100, 255, 0, 1)",
          width: 2,
        }),
        fill: new Fill({
          color: "rgba(100, 255, 0, 0.3)",
        }),
      }),
    });
    const previewLine = new Feature({
      geometry: new LineString([]),
    });
    const previewVector = new VectorLayer({
      source: new VectorSource({
        features: [previewLine],
      }),
      style: new Style({
        stroke: new Stroke({
          color: "rgba(255, 0, 0, 1)",
          width: 2,
        }),
      }),
    });
    const map = new Map({
      layers: [raster, baseVector, drawVector, previewVector],
      target: this.$refs.map,
      view: new View({
        center: [-12986427, 5678422],
        zoom: 5,
      }),
    });
    let drawInteraction, tracingFeature, startPoint, endPoint;
    let drawing = false;
    const getFeatureOptions = {
      hitTolerance: 10,
      layerFilter: (layer) => {
        return layer === baseVector;
      },
    };
    map.on("click", (event) => {
      if (!drawing) {
        return;
      }
      let hit = false;
      map.forEachFeatureAtPixel(
        event.pixel,
        (feature) => {
          if (tracingFeature && feature !== tracingFeature) {
            return;
          }
          hit = true;
          const coord = map.getCoordinateFromPixel(event.pixel);
          if (feature === tracingFeature) {
            endPoint = tracingFeature.getGeometry().getClosestPoint(coord);
            const appendCoords = getPartialRingCoords(
              tracingFeature,
              startPoint,
              endPoint
            );
            drawInteraction.removeLastPoint();
            drawInteraction.appendCoordinates(appendCoords);
            tracingFeature = null;
          }
          tracingFeature = feature;
          startPoint = tracingFeature.getGeometry().getClosestPoint(coord);
        },
        getFeatureOptions
      );
      if (!hit) {
        previewLine.getGeometry().setCoordinates([]);
        tracingFeature = null;
      }
    });
    map.on("pointermove", (event) => {
      if (tracingFeature && drawing) {
        let coord = null;
        map.forEachFeatureAtPixel(
          event.pixel,
          (feature) => {
            if (tracingFeature === feature) {
              coord = map.getCoordinateFromPixel(event.pixel);
            }
          },
          getFeatureOptions
        );
        let previewCoords = [];
        if (coord) {
          endPoint = tracingFeature.getGeometry().getClosestPoint(coord);
          previewCoords = getPartialRingCoords(
            tracingFeature,
            startPoint,
            endPoint
          );
        }
        previewLine.getGeometry().setCoordinates(previewCoords);
      }
    });
    const snapInteraction = new Snap({
      source: baseVector.getSource(),
    });
    const typeSelect = this.$refs.type;
    function addInteraction() {
      const value = typeSelect.value;
      if (value !== "None") {
        drawInteraction = new Draw({
          source: drawVector.getSource(),
          type: typeSelect.value,
        });
        drawInteraction.on("drawstart", () => {
          drawing = true;
        });
        drawInteraction.on("drawend", () => {
          drawing = false;
          previewLine.getGeometry().setCoordinates([]);
          tracingFeature = null;
        });
        map.addInteraction(drawInteraction);
        map.addInteraction(snapInteraction);
      }
    }
    typeSelect.onchange = function () {
      map.removeInteraction(drawInteraction);
      map.removeInteraction(snapInteraction);
      addInteraction();
    };
    addInteraction();
  },
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239