D3 が TopoJSON を正しくレンダリングしないのはなぜですか?

市民

私はスイスのマップをレンダリングしようとしているD3.jsTopoJSON基になる JSON はここにあります最初にこのチュートリアルに従おうとしましたがリモートで地図のように見えるものをレンダリングできなかったので、こここの質問と実際の例へのリンクを見つけました私が現在使用しているこのコードを取得した場所から:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
    <script src="http://d3js.org/topojson.v1.min.js"></script>
    <style>
    path {
        fill: #ccc;
    }
    </style>
</head>

<body>
    <h1>topojson simplified Switzerland</h1>
    <script>
    var width = window.innerWidth,
        height = window.innerHeight;

    var svg = d3.select("body")
        .append("svg")
        .attr("width", width)
        .attr("height", height);

    var projection = d3.geo.mercator()
        .scale(500)
        .translate([-900, 0]);

    var path = d3.geo.path()
        .projection(projection);

    d3.json("ch-cantons.json", function(error, topology) {
        if (error) throw error;

        svg.selectAll("path")
         .data(topojson.feature(topology, topology.objects.cantons).features).enter().append("path").attr("d", path);
    });
    </script>
</body>

</html>

以来JSON(貼り付け/コピーした後、それを正しくレンダリングするいくつかのオンラインプラットフォーム上で確認)と間違って行くことができる唯一の少しのコードがあるルックスの[OK]を、私はエラーが投影パラメータであると仮定します。少しいじってもうまくいきませんでした。ですから、どんな助けでも大歓迎です!

アンドリュー・リード

その通りです。エラーは投影法にあります。ただし、エラーは、データが投影されているか投影されていないか (緯度経度ペア) によって異なります。

予測されていないデータ

WGS84 にあるデータ、つまり緯度経度のペアがある場合、次の問題が発生します。

投影法を使用して、データ ソースのみを変更すると、次のような結果が得られます (右側の空の海を削りました)。

ここに画像の説明を入力してください

To center a Mercator properly, you need to know the center coordinate of your area of interest. This generally can be fairly general, for Switzerland I might try 47N 8.25E.

Once you have this coordinate you need to place it in the middle. One way is to rotate on the x axis and center on the y:

var projection = d3.geo.mercator()
    .scale(3500)
    .rotate([-8.25,0])
    .center([0,47])
    .translate([width/2,height/2])

Note that the x rotation is negative, you are spinning the globe underneath the projection.

The other option is to rotate on both x and y, this is likely the preferred option as you approach the poles - as Mercator distortion becomes unworkable at high latitudes. This approach would look like:

var projection = d3.geo.mercator()
    .scale(4000)
    .rotate([-8.25,-47])
    .center([0,0])
    .translate([width/2,height/2])

Note again the negative rotation values. The second option requires a higher scale value as this method essentially treats Switzerland as though it were at zero,zero on a Mercator projection - and along the equator land sizes are minimized.

Using the second of these projections, I get: ここに画像の説明を入力してください

So you'll have to dial in the scale a bit, but now you should be able to see your data (assuming your data is in proper lat long pairs).

Projected Data

Based on the comment below, which includes a linked json file, we can see that this is your problem.

There are two potential solutions to this:

  1. Convert the data to lat long pairs
  2. Use a geoTransform

Option one is the easiest, you'll unproject the data - which requires knowing the current projection. In GIS software this will generally be projecting it as WGS84, which is arguably not really a projection but a datum. Once you have your lat long pairs, you follow the steps above for unprojected data.

Option two skips a d3.geoProjection altogether. Instead, we'll create a transform function that will convert the projected coordinates to the desired SVG coordinates.

A geo projection looks like:

function scale (scaleFactor) {
    return d3.geo.transform({
        point: function(x, y) {
            this.stream.point(x * scaleFactor, (y * scaleFactor);
        }
    });
}

And is used like a projection:

var path = d3.geo.path().projection(scale(0.1));

It simply takes a stream of x,y coordinates that are already cartesian and transforms them in a specified manner.

To translate the map so it is centered you'll need to know the center coordinate. You can find this with path.bounds:

var bounds = path.bounds(features);
var centerX = (bounds[0][0] + bounds[1][0])/2;
var centerY = (bounds[0][1] + bounds[1][1])/2;

Path.bounds returns the top left corner and bottom right corner of a feature. This is the key part, you can make an autoscaling function, there are plenty of examples out there, but I like manually scaling often. If the map is centered, this is easy. For your map, your geoTransform might look like:

function scale (scaleFactor,cx,cy,width,height) {
    return d3.geo.transform({
        point: function(x, y) {
            this.stream.point((x-cx) * scaleFactor + width/2, (y-cy)  * scaleFactor +height/2);
        }
    });
}

ここcxcySVG要素の幅と高さを参照してくださいあなたの機能および幅と高さの真ん中を参照してください-私たちはSVGポイント[0,0]でクラスタ化された機能を望んでいません。

全体として、次のようなものになります (0.002 の倍率で):

ここに画像の説明を入力してください

更新された JSbin は次のとおりです: http://jsbin.com/wolamuzeze/edit?html,output

幅/高さはあなたのケースではウィンドウ サイズに関連しているため、スケールはウィンドウ サイズに依存していることに注意してください。これは、ズーム レベルを自動的に設定することで最もよく対処できますが、ラベルがある場合などに問題が発生する可能性があります。

この回答も役立つかもしれません: SVG (またはまったく) に合わせて d3 v4 マップをスケーリングする

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

D3マップがオーストラリアのtopojsonファイルをレンダリングしない

分類Dev

D3を使用したTopoJSONレンダリングの問題

分類Dev

D3はgeojsonのパスを追加しますが、topojsonは追加しません

分類Dev

土地の座標情報がある場合、d3でtopojsonを使用して海を色付けするにはどうすればよいですか?

分類Dev

土地の座標情報がある場合、d3でtopojsonを使用して海を色付けするにはどうすればよいですか?

分類Dev

D3リンクラベルが機能しなくなったのはなぜですか

分類Dev

D3コード図が正しくレンダリングされない

分類Dev

topojson / D3 /経度+緯度の地図

分類Dev

等期間のd3トランジションが同期しなくなるのはなぜですか?

分類Dev

D3ドラッグが機能しないのはなぜですか?

分類Dev

OpenGL(3)の三角形がlwjglを使用してレンダリングされないのはなぜですか?

分類Dev

D3がJSONファイルでマップデータをレンダリングしない

分類Dev

D3のChoroplethの例で使用するTopoJSONの生成の問題

分類Dev

JavaFX3D深度レンダリングが正しくない

分類Dev

d3 / topojsonパス要素の図心を取得できません

分類Dev

IE と Chrome で CSS と HTML が正しくレンダリングされないのはなぜですか?

分類Dev

.each()がD3で機能しないのはなぜですか?

分類Dev

このviewmodel値が正しくレンダリングされないのはなぜですか?

分類Dev

D3TopojsonがFeatureCollectionにNameプロパティを追加する

分類Dev

SVGが正しくレンダリングされないのはなぜですか?

分類Dev

SceneViewが正しくレンダリングされないのはなぜですか

分類Dev

SceneViewが正しくレンダリングされないのはなぜですか

分類Dev

Unicode文字が正しくレンダリングされないのはなぜですか

分類Dev

レスポンシブD3チャートでグリッド線の一部がランダムに消えるのはなぜですか?

分類Dev

ReactコンポーネントがHTMLをレンダリングしないのに、正しくログに記録されるのはなぜですか?

分類Dev

d3.jsがgeojsonデータを正しくレンダリングしない

分類Dev

D3で素晴らしいフォントを使用するとリンクがレンダリングされない

分類Dev

D3TopoJSON変換を理解する

分類Dev

ulのliのインデックスですが、JqueryではなくD3を使用しています

Related 関連記事

  1. 1

    D3マップがオーストラリアのtopojsonファイルをレンダリングしない

  2. 2

    D3を使用したTopoJSONレンダリングの問題

  3. 3

    D3はgeojsonのパスを追加しますが、topojsonは追加しません

  4. 4

    土地の座標情報がある場合、d3でtopojsonを使用して海を色付けするにはどうすればよいですか?

  5. 5

    土地の座標情報がある場合、d3でtopojsonを使用して海を色付けするにはどうすればよいですか?

  6. 6

    D3リンクラベルが機能しなくなったのはなぜですか

  7. 7

    D3コード図が正しくレンダリングされない

  8. 8

    topojson / D3 /経度+緯度の地図

  9. 9

    等期間のd3トランジションが同期しなくなるのはなぜですか?

  10. 10

    D3ドラッグが機能しないのはなぜですか?

  11. 11

    OpenGL(3)の三角形がlwjglを使用してレンダリングされないのはなぜですか?

  12. 12

    D3がJSONファイルでマップデータをレンダリングしない

  13. 13

    D3のChoroplethの例で使用するTopoJSONの生成の問題

  14. 14

    JavaFX3D深度レンダリングが正しくない

  15. 15

    d3 / topojsonパス要素の図心を取得できません

  16. 16

    IE と Chrome で CSS と HTML が正しくレンダリングされないのはなぜですか?

  17. 17

    .each()がD3で機能しないのはなぜですか?

  18. 18

    このviewmodel値が正しくレンダリングされないのはなぜですか?

  19. 19

    D3TopojsonがFeatureCollectionにNameプロパティを追加する

  20. 20

    SVGが正しくレンダリングされないのはなぜですか?

  21. 21

    SceneViewが正しくレンダリングされないのはなぜですか

  22. 22

    SceneViewが正しくレンダリングされないのはなぜですか

  23. 23

    Unicode文字が正しくレンダリングされないのはなぜですか

  24. 24

    レスポンシブD3チャートでグリッド線の一部がランダムに消えるのはなぜですか?

  25. 25

    ReactコンポーネントがHTMLをレンダリングしないのに、正しくログに記録されるのはなぜですか?

  26. 26

    d3.jsがgeojsonデータを正しくレンダリングしない

  27. 27

    D3で素晴らしいフォントを使用するとリンクがレンダリングされない

  28. 28

    D3TopoJSON変換を理解する

  29. 29

    ulのliのインデックスですが、JqueryではなくD3を使用しています

ホットタグ

アーカイブ