JViews Maps で使用できる定義済みのリーダーに加え、独自のリーダーを書き込み、カスタマイズすることができます。
このセクションでは IlvMapFeatureIterator の例を紹介します。この例では、ASCII ファイルに保存されている折れ線を読み込むことができます。
メモ
IlvMapFeatureIterator
インターフェースを実装するクラスは、必ずしもファイル・リーダーとは限りません。例えば、マップ・サーバーへの照会結果について反復処理を行うことができます。読み込むファイル
読み込む ASCII ファイルは、特にこの例のために作成されています。その形式は非常に単純で、仕様は以下のとおりです。
- このファイルには、形式を特定するヘッダーがあります。
- 1 行ごとに 1 組の座標 (緯度と経度) があります。これらの座標は度単位で表されます。
- 行にコメントが含まれている場合があります。コメントがある場合、それらのコメントはマージされて属性になります。
- 折れ線は空白行で分離されています。
- ファイルには、
.pol
拡張子が付いています。
以下は ASCII ファイルです。
ascii polylines -1.0 40.0 A 1x1 degree rectangle centered on the 1.0 40.0 (0,39) point 1.0 38.0 -1.0 38.0 -1.0 40.0 0.0 90.0 A meridian extending from the North pole to the South pole 0.0 -90.0
リーダー
このセクションでは、この折れ線ファイルの読み込みに使用可能なリーダーについて説明します。
この例のソース・コード一式は、以下のファイルにあります。
メモ
ここでは、コメントを必要とするコード部分のみを再現しています。
以下に示すように、
SimplePolylineReader
は IlvMapFeatureIterator インターフェースを実装します。class SimplePolygonReader implements IlvMapFeatureIterator { ... member variables ... /** * Constructor */ public SimplePolygonReader (String fileName) throws FileNotFoundException { Initializing member variables } ... methods ... }
地理参照メソッド
折れ線ファイル内の緯度および経度が度で表現されているので、座標系は地理座標系です。このため、 isGeoreferenced メソッドは
true
を返し、 getCoordinateSystem メソッドは IlvGeographicCoordinateSystem.WGS84
を返します。getCoordinateSystem
メソッドは、読み取るファイルの投影図法が不明の場合に、null
を返す場合があります。「IlvMapFeatureIterator インターフェース」の isGeoreferenced
メソッドの説明を参照してください。public boolean isGeoreferenced() { return true; } public IlvCoordinateSystem getCoordinateSystem() { return IlvGeographicCoordinateSystem.WGS84; }
バウンディング・ボックス・メソッド
データ形式によって、データが読み込まれるまで折れ線のバウンディング・ボックスを取得できません。ここで、メソッド getUpperLeftCorner および getLowerRightCorner は
null
を返し、これらのポイントが不明であることを示します。また、データを読み込んで、配列に置き、バウンディング・ボックスを計算する方法もあります。public IlvCoordinate getLowerRightCorner() { return null; }
レンダリング・メソッド
getDefaultFeatureRenderer メソッドは、すべての地図機能がこの機能イテレーターで読み込まれるグラフィック・オブジェクトに変換可能なレンダラーを返す必要があります。 IlvDefaultCurveRenderer は、タイプ IlvMapLineString.
public IlvFeatureRenderer getDefaultFeatureRenderer() { return new IlvDefaultCurveRenderer(); }
返された地図機能のジオメトリーが定義されておらず、代わりに、派生クラスのインスタンスである場合、または、地図機能の属性が、色や線の幅などのレンダリング処理に使用する描画パラメーターを格納する場合、これらの属性や派生ジオメトリーを処理できるレンダラーを提供する必要があります。色付き線のレンダラーの作成 を参照してください。
getNextFeature メソッド
getNextFeature メソッドは、地図機能のジオメトリーを読み込み、ジオメトリーの処理に必要なすべての情報を保持する IlvMapFeature オブジェクトを作成します。次のコード例で読み込むジオメトリーは、折れ線ジオメトリーを定義するクラスの IlvMapLineString です。
public IlvMapFeature getNextFeature() throws IOException { return readPolyline(); }
折れ線のポイントは、プライベート・メソッド
readPolyline
で読み込まれます。このメソッドは、ファイルの各行を読み込み、ポイントと関連するコメントの座標がある場合に抽出します。詳細は、次のようになります。
- タイプ IlvMapLineString のジオメトリーを作成し、地図機能に関連付けます。
private IlvMapFeature readPolyline() throws IOException { // Concatenates all the comment lines. StringBuffer buffer = new StringBuffer(); // Reads the current map feature. IlvMapFeature feature = new IlvMapFeature(); // Reads the current line string geometry. IlvMapLineString geometry = new IlvMapLineString();
- この行ストリングを構成するポイントを読み込み、関連するコメントを属性として保存します。
// Stores the line of text that is read. String line; // Reads a line. while ((line = file.readLine()) != null) { [...]
- 経度座標値を読み込みます。読み込み中に形式エラーが検出されると、タイプ IlvMapFormatException の例外が発せられます。
// Process longitude. IlvCoordinate c = new IlvCoordinate(); if (tokenizer.hasMoreElements() == false) throw new IlvMapFormatException("Longitude coordinate expected"); try { currentToken = (String)tokenizer.nextElement(); c.x = decimalParser.parse(currentToken).doubleValue(); } catch (ParseException e) { throw new IlvMapFormatException("Error while parsing longitude"); }
また、これらのコメントは経度座標に適用されます。 - ファイルから読み込まれた各ポイントを行ストリングのジオメトリーに追加します。
// Add this point to geometry. geometry.addPoint(c); [...]
- 以下の
if
ステートメントでは、ファイルの末尾に達したかどうかをテストします。この場合、getNextFeature
メソッドはnull
ポインターを返します。// End of file. if ((line == null) && (geometry.getPointCount() == 0)) return null;
- ジオメトリーを地図機能に関連付けます。
// Initialize the map feature. feature.setGeometry(geometry);
- 地図機能に関連付けるフォーム属性へコメントを抽出します。地図機能の属性で共有される
attributeInfo
オブジェクトが、リーダーのコンストラクターで初期化されています。// Set attribute. IlvStringAttribute[] attribute = new IlvStringAttribute[1]; if (buffer.length() > 0) { attribute[0] = new IlvStringAttribute(); attribute[0].setString(buffer.toString()); } else { attribute[0] = null; } feature.setAttributeInfo(attributeInfo); feature.setAttributes(new IlvFeatureAttributeProperty(attributeInfo, attribute));
- 地図機能が返されます。
// Returns the read map feature. return feature; }