How to use the ol/format/GeoJSON function in ol

To help you get started, we’ve selected a few ol 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 3liz / lizmap-web-client / assets / src / modules / Digitizing.js View on Github external
}
                OL6feature = new Feature(new LineString(coordinates));
            }
            else if (featureGeometry.CLASS_NAME === 'OpenLayers.Geometry.Polygon'){
                let coordinates = [];
                for (const component of featureGeometry.components[0].components) {
                    coordinates.push([component.x, component.y]);
                }
                OL6feature = new Feature(new Polygon([coordinates]));
            }

            // Reproject to EPSG:4326
            OL6feature.getGeometry().transform(mainLizmap.projection, 'EPSG:4326');

            if(format === 'geojson'){
                const geoJSON = (new GeoJSON()).writeFeature(OL6feature);

                this._downloadString(geoJSON, 'application/geo+json', 'export.geojson');
            }
            else if(format === 'gpx'){
                const gpx = (new GPX()).writeFeatures([OL6feature]);

                this._downloadString(gpx, 'application/gpx+xml', 'export.gpx');
            } else if (format === 'kml') {
                const kml = (new KML()).writeFeatures([OL6feature]);

                this._downloadString(kml, 'application/vnd.google-earth.kml+xml', 'export.kml');
            }
        }
    }
github geosolutions-it / MapStore2 / web / client / components / map / openlayers / Feature.jsx View on Github external
addFeatures = (props) => {
        const format = new GeoJSON();
        let ftGeometry = null;
        let canRender = false;

        if (props.type === "FeatureCollection") {
            ftGeometry = { features: props.features };
            canRender = !!(props.features);
        } else {
            // if type is geometryCollection or a simple geometry, the data will be in geometry prop
            ftGeometry = { geometry: props.geometry };
            canRender = !!(props.geometry && (props.geometry.geometries || props.geometry.coordinates));
        }

        if (props.container && canRender) {
            this._feature = format.readFeatures({
                type: props.type,
                properties: props.properties,
github geomoose / gm3 / src / gm3 / components / map.js View on Github external
// get the map source
        const ms_name = util.getMapSourceName(queryLayer);

        const map_source = this.props.mapSources[ms_name];

        // if the openlayers layer is not on, this fakes
        //  one for use in the query.
        let ol_layer = this.olLayers[ms_name];
        if(!ol_layer) {
            ol_layer = this.createLayer(map_source);
        }

        // get the src
        const src = ol_layer.getSource();

        const format = new GeoJSONFormat();

        const result_features = [];

        const selection = query.selection;
        if(selection && selection.geometry && selection.geometry.type === 'Point') {
            const coords = selection.geometry.coordinates;
            src.forEachFeatureAtCoordinateDirect(coords, (feature) => {
                result_features.push(format.writeFeatureObject(feature));
            });
        }

        this.props.store.dispatch(
            mapActions.resultsForQuery(queryId, queryLayer, false, result_features)
        );

        this.checkQueryForCompleteness(queryId, queryLayer);
github SuperMap / iClient-JavaScript / test / openlayers / services / EditFeaturesSpec.js View on Github external
it('updateFeature', (done) => {
        var updateFeatureResult = null;
        if (originFeature != null) {
            var random = parseInt(Math.random() * 10000000);
            originFeature.properties.LANDTYPE = "用材林" + random;
            var data = new GeoJSON().readFeatures(originFeature);
            var updateFeaturesService = new FeatureService(editServiceURL);
            var updateFeaturesParams = new EditFeaturesParameters({
                dataSourceName: "Jingjin",
                dataSetName: "Landuse_R",
                features: data,
                editType: "update"
            });
            spyOn(FetchRequest, 'commit').and.callFake((method, testUrl, params, options) => {
                expect(method).toBe("PUT");
                expect(testUrl).toBe(editServiceURL + "/datasources/Jingjin/datasets/Landuse_R/features.json?");
                expect(params).not.toBeNull();
                var paramsObj = JSON.parse(params.replace(/'/g, "\""));
                expect(paramsObj[0].fieldNames[0]).toBe("SMID");
                expect(options).not.toBeNull();
                return Promise.resolve(new Response(`{"succeed":true}`));
            });
github nextgis / nextgisweb_frontend / packages / ol-map-adapter / src / layer-adapters / GeoJsonAdapter.ts View on Github external
addData(data: GeoJsonObject) {
    const features = new GeoJSON().readFeatures(data, {
      dataProjection: 'EPSG:4326',
      featureProjection: 'EPSG:3857'
    });
    this._features = this._features.concat(features);
    if (this._filterFun) {
      this.filter(this._filterFun);
    } else {
      this.vectorSource.addFeatures(features);
    }
  }
github nasa / common-mapping-client / src / _core / utils / MapWrapperOpenlayers.js View on Github external
if (
            typeof options.url !== "undefined" &&
            typeof layer.getIn(["urlFunctions", appStrings.MAP_LIB_2D]) !== "undefined"
        ) {
            let urlFunction = this.tileHandler.getUrlFunction(
                layer.getIn(["urlFunctions", appStrings.MAP_LIB_2D])
            );
            options.url = urlFunction({
                layer: layer,
                url: options.url
            });
        }

        return new Ol_Source_Vector({
            url: options.url,
            format: new Ol_Format_GeoJSON()
        });
    }
github nasa-gibs / worldview / web / js / map / data / map.js View on Github external
var updateGrid = function() {
    gridLayer.getSource().clear();
    var grid = results.meta.grid;
    if (!grid) {
      return;
    }
    var features = [];
    var parser = new OlFormatGeoJSON();
    lodashEach(grid, function(cell) {
      var geom = parser.readGeometry(cell.geometry);
      var feature = new OlFeature(geom);
      features.push(feature);
    });
    gridLayer.getSource().addFeatures(features);
  };
github infra-geo-ouverte / igo2-lib / packages / geo / src / lib / feature / shared / feature.utils.ts View on Github external
let geom;
    let title;
    let exclude;
    let excludeOffline;

    if (olLayer) {
      title = olLayer.get('title');
      if (olLayer.get('sourceOptions')) {
        exclude = olLayer.get('sourceOptions').excludeAttribute;
        excludeOffline = olLayer.get('sourceOptions').excludeAttributeOffline;
      }
    } else {
      title = olRenderFeature.get('_title');
    }

    const olFormat = new OlFormatGeoJSON();
    const properties = olRenderFeature.getProperties();
    const geometryType = olRenderFeature.getType();

    if (geometryType === 'Polygon') {
      const ends = olRenderFeature.ends_;
      geom = new OlPolygon(olRenderFeature.flatCoordinates_, OlGeometryLayout.XY, ends);
    } else if (geometryType === 'Point') {
      geom = new OlPoint(olRenderFeature.flatCoordinates_, OlGeometryLayout.XY);
    } else if (geometryType === 'LineString') {
      geom = new OlLineString(olRenderFeature.flatCoordinates_, OlGeometryLayout.XY);
    }

    const geometry = olFormat.writeGeometryObject(geom, {
      dataProjection: projectionOut,
      featureProjection: projectionIn
    });
github openlayers / ol-mapbox-style / index.js View on Github external
unByKey(key);
      layer.setSource(undefined);
    }
  });
  source.setTileLoadFunction(function(tile, src) {
    if (src.indexOf('{bbox-epsg-3857}') != -1) {
      const bbox = source.getTileGrid().getTileCoordExtent(tile.getTileCoord());
      src = src.replace('{bbox-epsg-3857}', bbox.toString());
    }
    const img = /** @type {import("ol/ImageTile").default} */ (tile).getImage();
    /** @type {HTMLImageElement} */ (img).src = src;
  });
  return layer;
}

const geoJsonFormat = new GeoJSON();
function setupGeoJSONLayer(glSource, path) {
  const data = glSource.data;
  let features, geoJsonUrl;
  if (typeof data == 'string') {
    geoJsonUrl = withPath(data, path);
  } else {
    features = geoJsonFormat.readFeatures(data, {featureProjection: 'EPSG:3857'});
  }
  return new VectorLayer({
    source: new VectorSource({
      attributions: glSource.attribution,
      features: features,
      format: geoJsonFormat,
      url: geoJsonUrl
    }),
    visible: false
github infra-geo-ouverte / igo2-lib / packages / geo / src / lib / routing / routing-form / routing-form.component.ts View on Github external
chooseProposal(proposal, i) {
    if (proposal !== undefined) {
      let geomCoord;
      const geom = (proposal as any).geometry;
      if (geom.type === 'Point') {
        geomCoord = geom.coordinates;
      } else if (geom.type.search('Line') >= 0) {
        const line = (new OlGeoJSON()).readFeatures(geom);
        geomCoord = line[0].getGeometry().getFirstCoordinate();
        geomCoord = [geomCoord[0], geomCoord[1]];
      } else if (geom.type.search('Polygon') >= 0) {
        const poly = (new OlGeoJSON()).readFeatures(geom);
        geomCoord = poly[0].getGeometry().getInteriorPoints().getFirstCoordinate();
        geomCoord = [geomCoord[0], geomCoord[1]];
      }

      if (geomCoord !== undefined) {
        this.stops.at(i).patchValue({ stopCoordinates: geomCoord });
        this.addStopOverlay(geomCoord, i);
        const proposalExtent = this.routingStopsOverlayDataSource.ol
          .getFeatureById(this.getStopOverlayID(i))
          .getGeometry()
          .getExtent();

        if (!olextent.intersects(proposalExtent, this.map.getExtent())) {
          this.map.viewController.moveToExtent(proposalExtent);
        }
      }