2.0.3 • Published 4 years ago

aframe-geojson-component v2.0.3

Weekly downloads
10
License
MIT
Repository
github
Last release
4 years ago

aframe-geojson-component

A GeoJSON/TopoJSON component using D3.js for easily building an orthographic globe in A-Frame.

Supports A-Frame 1.0.x.

Example

API

This library provides two components visualizing GeoJSON and TopoJSON documents as an orthographic globe. It can visualize MultiPoints/Points as a point cloud, MultiLineStrings/LineStrings as lines and MultiPolygons/Polygons as an outline (no triangulated meshes!).

geojson component

The geojson component has the material and geometry components as a dependency from the entity. It implements a raycaster logic that fires an event if a GeoJSON feature gets selected.

Schema
PropertyDescriptionDefault ValueValues
srcThe GeoJSON/TopoJSON asset.
projectionOne of D3's projection. Use the function name, like "geoEquirectangular" or "geoOrthographic". Use "geoEquirectangular" if you have global data and want to wrap it around a sphere.geoEquirectangularSee projections
topologyObjctOnly for TopoJSON: Specifies the TopoJSON object to use (see specification for details). If empty then the first will be taken.""
featureKeyThe primary key of the GeoJSON feature properties. Choose as key the property that has unique values. Must be given, otherwise nothing will be shown. For TopoJSON this is by specification always the id property . For GeoJSON you have to select the primary keyid
dataSrcThe optional metadata that of this GeoJSON document. Mostly GeoJSON contains already all the associated data, however TopoJSON have always associated data sources.
dataTypeThe data type of the dataSrc attribute. Only needed if dataSrc is specified.csvcsv, tsv
dataKeyThe name of the property of the metadata that is matched to the GeoJSON's featureKey attribute. Only needed if dataSrc is specified.id
raycasterAccuracyThe "accuracy" of the raycaster selection. The smaller the closer you have to point with raycaster on the feature to be selected. Set smaller if you have many features close by, higher otherwise. Normalized to a generally good working condition. Unfortunately point features are not respected ...1
featureEventNameThe event name that should be added as a listener. No events will be omitted if empty.click, raycaster-intersected, mousedown, mouseup, ...
lineWidthThe width of the line for Polygons and Line elements.1
pointScaleThe scaling factor of the Point data0.1
pointSizeFeatureSets the size of the point using a feature. Optional.
pointAsShows the Point data either as a point cloud or as scaled line.pointpoint, line
omitBoundingBoxSome GeoJSON files have a bounding box border around that gives a straight line when projected on a sphere. When true then this border will be ignored.false
Events
NameDataDescription
geojson-loaded{ data }Fired when the GeoJSON has finished loading (asynchronous operation!). data contains the generated GeoJSON and graphical data.
geojson-feature-selected{ feature, mesh, point }The selected feature and mesh by the raycaster if the featureEventName parameter is given. To highlight the mesh you have to configure its material property and add it to the scene. This works only with the first enabled raycaster!
API
NameDataDescription
getMaskMeshReturns the mesh of the hit mask. When your geometry of the component changes (like rotation, or scaling) you must also set this changes to the mask mesh. See examples for a use case.
getDataReturns a map the contains for each primary key the properties object, the original 2D point and the 3D world space position and if applicable the shape Mesh of polygon data types. May be used to display your own point visualization rather than using the default simple point visualization.

geojson-texture component

This component so far can be just used as a texture for a geometry object (plane, sphere, etc). No feature selection events are fired. A strategy may be implemented in future releases.

Schema
PropertyDescriptionDefault ValueValues
srcThe GeoJSON/TopoJSON asset.
topologyObjctOnly for TopoJSON: Specifies the TopoJSON object to use (see specification for details). If empty then the first will be taken.
canvasThe DOM canvas to use as the texture. Please set width and height to power of twos
projectionOne of D3's projection or D3's geo projection. Use the function name, like "geoEquirectangular" or "geoOrthographic". Use "geoEquirectangular" if you have global data and want to wrap it around a sphere.'geoEquirectangular'
fillColorPolygon filling color#fff
fillOpacityOpacity of the polygons1
lineColorLine color#fff
lineOpacityLine opacity1
centerThe center of the projection. See the D3 documentation for details and use cases.0 0
rotationThe rotation of the projection. See the D3 documentation for details and use cases.0 0
Events
NameDataDescription
geojson-texture-generatedNoneFired when the GeoJSON has finished loading (asynchronous operation!) or when the style has changed and the canvas is redrawn. You must set the needsUpdate to true on your mesh map to be reflected on the mesh!
API
NameDataDescription
getProjectionNoneReturns the referenced D3 projection object that may be used for calculations or transformations.
drawNoneRedraws the canvas.

Styling

The library respects the commonly used GeoJSON Styling Spec. Although the spec above does not support dashed lines, it would not work anyway, because dashed lines do not work with BufferGeometry's.

Further Styling

To change the the appearnance of the points or lines just register custom material

  • Polygons and Lines need a THREE.LineBasicMaterial
  • Points need a THREE.PointsMaterial

Installation

Browser

Install and use by directly including the browser files:

<head>
  <title>My A-Frame Scene</title>
  <script src="https://aframe.io/releases/1.0.3/aframe.min.js"></script>
  <script src="https://d3js.org/d3.v5.js"></script>
  <script src="https://unpkg.com/aframe-geojson-component/dist/aframe-geojson-component.min.js"></script>
</head>

<body>
  <a-scene>
    <a-assets>
    <a-asset-item id="world-geojson" src="assets/world-50m.v1.json" />
    </a-assets>        

    <a-entity
      geometry="primitive: sphere; radius: 1;"
      material="color: #000;"
      geojson="
        src: #world-geojson; 
        topologyObject: countries;
        "
      />
  </a-scene>
</body>

npm

Install via npm:

npm install aframe-geojson-component

Then require and use.

require('aframe');
require('aframe-geojson-component');

Large scale geometry data

If you have a GeoJSON where the geometry bounds are on a large scale, then the output will be very tiny. You have to rotate the entity and scale it up so that you can see your local data. See the europe example for instance for a larger scale geometry. However this component is primarly for displaying orthographic low scale data (which covers the whole world). For other data its probably wiser to use a different component, like the Mapbox component or Tangram component.

Antimeridian Cutting

Most GeoJSON objects might show a line on the antimeridian when projected on a sphere with the equirectangular projection.

To prevent this the original GeoJSON object has to be stitched manually beforehand.

See

Implementation details

The componenet uses the D3 library to convert the GeoJSON/TopoJSON into a SVG. The SVG is then converted into line and point _BufferGeometry_s.

To be able to interact with the generated geometires an invisble hit mask is generated for all GeoJSON polygon, line and points to obtain the selected geojson feature via GPU Picking technique (much faster than raycasting).

Polygon hitmask

or for point geometries

Point hitmask

and finally for line geometries

Line hitmask

Those various masks that are generated depending on the GeoJSON contents get then rendered off-scene and picked by the GPU.

I have seen this use case implemented by the great @thespite and enhanced it for more use cases.

2.0.3

4 years ago

2.0.2

4 years ago

2.0.0

4 years ago

1.4.4

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.6

5 years ago

1.3.5

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.13

6 years ago

1.2.12

6 years ago

1.2.11

6 years ago

1.2.10

6 years ago

1.2.9

6 years ago

1.2.8

6 years ago

1.2.7

6 years ago

1.2.6

6 years ago

1.2.5

6 years ago

1.2.4

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago