Skip to content

Commit

Permalink
start of vector import support
Browse files Browse the repository at this point in the history
  • Loading branch information
systemed committed Dec 17, 2009
1 parent 2d2525a commit e574c19
Show file tree
Hide file tree
Showing 25 changed files with 1,388 additions and 26 deletions.
19 changes: 19 additions & 0 deletions net/systemeD/halcyon/ExtendedURLLoader.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.systemeD.halcyon {

/*
=== ExtendedURLLoader ===
This simply allows us to store arbitrary data (e.g. a filename) in the URLLoader object,
so that the responder knows which image has just been loaded.
*/

import flash.events.*;
import flash.net.*;
import flash.display.*;

public class ExtendedURLLoader extends URLLoader {
public var info:Object=new Object();
}

}
19 changes: 0 additions & 19 deletions net/systemeD/halcyon/ImageURLLoader.as

This file was deleted.

2 changes: 1 addition & 1 deletion net/systemeD/halcyon/Map.as
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package net.systemeD.halcyon {
import net.systemeD.halcyon.connection.EntityEvent;
import net.systemeD.halcyon.styleparser.*;
import net.systemeD.halcyon.Globals;

// for experimental export function:
// import flash.net.FileReference;
// import com.adobe.images.JPGEncoder;
Expand Down
2 changes: 0 additions & 2 deletions net/systemeD/halcyon/TileSet.as
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package net.systemeD.halcyon {
import flash.net.*;
import flash.system.LoaderContext;

import net.systemeD.halcyon.ImageURLLoader;

public class TileSet extends Sprite {

public var tile_l:int;
Expand Down
1 change: 1 addition & 0 deletions net/systemeD/halcyon/connection/Entity.as
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package net.systemeD.halcyon.connection {
private var modified:Boolean = false;
private var _loaded:Boolean = true;
private var parents:Dictionary = new Dictionary();
private var locked:Boolean = false;

public function Entity(id:Number, version:uint, tags:Object, loaded:Boolean) {
this._id = id;
Expand Down
8 changes: 4 additions & 4 deletions net/systemeD/halcyon/styleparser/RuleSet.as
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package net.systemeD.halcyon.styleparser {
import flash.events.*;
import flash.net.*;
import net.systemeD.halcyon.Map;
import net.systemeD.halcyon.ImageURLLoader;
import net.systemeD.halcyon.ExtendedURLLoader;
import net.systemeD.halcyon.connection.Entity;

import net.systemeD.halcyon.connection.*;
Expand Down Expand Up @@ -82,9 +82,9 @@ package net.systemeD.halcyon.styleparser {

iconsToLoad++;
var request:URLRequest=new URLRequest(filename);
var loader:ImageURLLoader=new ImageURLLoader();
var loader:ExtendedURLLoader=new ExtendedURLLoader();
loader.dataFormat=URLLoaderDataFormat.BINARY;
loader.filename=filename;
loader.info['filename']=filename;
loader.addEventListener(Event.COMPLETE, loadedImage, false, 0, true);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
Expand All @@ -97,7 +97,7 @@ package net.systemeD.halcyon.styleparser {
// data handler

private function loadedImage(event:Event):void {
images[event.target.filename]=event.target.data;
images[event.target.info['filename']]=event.target.data;
iconsToLoad--;
if (iconsToLoad==0 && iconCallback!=null) { iconCallback(); }
}
Expand Down
53 changes: 53 additions & 0 deletions net/systemeD/potlatch2/utils/Importer.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package net.systemeD.potlatch2.utils {

import net.systemeD.halcyon.Map;
import net.systemeD.halcyon.ExtendedURLLoader;
import flash.display.LoaderInfo;
import flash.events.*;
import flash.net.*;

import net.systemeD.halcyon.Globals;

public class Importer {

protected var map:Map;
protected var files:Array=[];
protected var filenames:Array;
protected var filesloaded:uint=0;

public function Importer(map:Map, filenames:Array) {
Globals.vars.root.addDebug("starting importer");
this.map = map;
this.filenames=filenames;

var sp:uint=0;
for each (var fn:String in filenames) {
Globals.vars.root.addDebug("requesting file "+fn);

var loader:ExtendedURLLoader = new ExtendedURLLoader();
loader.info['file']=sp;
loader.dataFormat=URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, fileLoaded, false, 0, true);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
loader.load(new URLRequest(fn));
sp++;
}
}

protected function fileLoaded(e:Event):void {
Globals.vars.root.addDebug("loaded file "+e.target.info['file']);
files[e.target.info['file']]=e.target.data;
filesloaded++;
if (filesloaded==filenames.length) { doImport(); }
}

protected function doImport():void { }

protected function httpStatusHandler( event:HTTPStatusEvent ):void { }
protected function securityErrorHandler( event:SecurityErrorEvent ):void { Globals.vars.root.addDebug("securityerrorevent"); }
protected function ioErrorHandler( event:IOErrorEvent ):void { Globals.vars.root.addDebug("ioerrorevent"); }

}
}
60 changes: 60 additions & 0 deletions net/systemeD/potlatch2/utils/ShpImporter.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.systemeD.potlatch2.utils {

import net.systemeD.halcyon.Map;
import net.systemeD.halcyon.connection.*;
import org.vanrijkom.shp.*;
import org.vanrijkom.dbf.*;

import net.systemeD.halcyon.Globals;

// SHP class docs and examples:
// http://vanrijkom.org/shp/index.html
// http://www.boxshapedworld.com/blog/post/Shapefiles-Actionscript-30-and-Google-Maps.aspx
// http://web.archive.org/web/20071119113250rn_1/vanrijkom.org/samples/fsd-mexico/srcview/
// we load .shp as files[0], .shx as files[1], .dbf as files[2]

// See http://www.actionscript.org/forums/showthread.php3?t=185320 for tips on avoiding time-outs with big files -
// probably needs to be asynchronous

public class ShpImporter extends Importer {

public function ShpImporter(map:Map, filenames:Array) {
super(map, filenames);
}

// All data is loaded, so do the import

override protected function doImport():void {
Globals.vars.root.addDebug("importing");
var shp:ShpHeader=new ShpHeader(files[0]);
var dbf:DbfHeader=new DbfHeader(files[2]);

if (shp.shapeType==ShpType.SHAPE_POLYGON || shp.shapeType==ShpType.SHAPE_POLYLINE) {

// Loop through all polylines in the shape
var polyArray:Array = ShpTools.readRecords(files[0]);
for (var i:uint=0; i<Math.min(polyArray.length,50); i++) {

// Get attributes like this:
// var dr:DbfRecord = DbfTools.getRecord(files[2], dbf, i);
// var xsID:String = dr.values[idFieldName];

// Do each ring in turn, then each point in the ring
for (var j:int=0; j < Math.min(polyArray[i].shape.rings.length,50); j++) {
var nodes:Array=[];
var points:Array = polyArray[i].shape.rings[j];
if (points!=null) {
for (var k:int=0; k < Math.min(points.length,50); k++) {
var p:ShpPoint = ShpPoint(points[k]);
var node:Node = map.connection.createNode({}, p.y, p.x);
nodes.push(node);
Globals.vars.root.addDebug("point "+p.x+","+p.y);
}
}
if (nodes.length>0) { var way:Way = map.connection.createWay({}, nodes); }
}
}
}
}
}
}
45 changes: 45 additions & 0 deletions org/vanrijkom/dbf/DbfError.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* ************************************************************************ */
/* */
/* DBF (XBase File Reader) */
/* Copyright (c)2007 Edwin van Rijkom */
/* http://www.vanrijkom.org */
/* */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */
/* */
/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
/* Lesser General Public License or the LICENSE file for more details. */
/* */
/* ************************************************************************ */

package org.vanrijkom.dbf
{

/**
* Instances of the DbfError class are thrown from the DBF library classes
* on encountering errors.
* @author Edwin van Rijkom
*
*/
public class DbfError extends Error
{
/**
* Defines the identifier value of an undefined error.
*/
public static const ERROR_UNDEFINED : int = 0;
/**
* Defines the identifier value of a 'out of bounds' error, which is thrown
* when an invalid item index is passed.
*/
public static const ERROR_OUTOFBOUNDS : int = 1;

public function DbfError(msg: String, id: int=0) {
super(msg,id);
}
}

} // package
101 changes: 101 additions & 0 deletions org/vanrijkom/dbf/DbfField.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* ************************************************************************ */
/* */
/* DBF (XBase File Reader) */
/* Copyright (c)2007 Edwin van Rijkom */
/* http://www.vanrijkom.org */
/* */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */
/* */
/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
/* Lesser General Public License or the LICENSE file for more details. */
/* */
/* ************************************************************************ */

package org.vanrijkom.dbf
{

import flash.utils.ByteArray;
import flash.utils.Endian;

/**
* The DbfField class parses a field definition from a DBF file loaded to a
* ByteArray.
* @author Edwin van Rijkom
*
*/
public class DbfField
{
/**
* Field name.
*/
public var name: String;
/**
* Field type.
*/
public var type: uint;
/**
* Field address.
*/
public var address: uint;
/**
* Field lenght.
*/
public var length: uint;
/**
* Field decimals.
*/
public var decimals: uint;
/**
* Field id.
*/
public var id: uint;
/**
* Field set flag.
*/
public var setFlag: uint;
/**
* Field index flag.
*/
public var indexFlag: uint;

/**
* Constructor.
* @param src
* @return
*
*/
public function DbfField(src: ByteArray) {

name = DbfTools.readZeroTermANSIString(src);

// fixed length: 10, so:
src.position += (10-name.length);

type = src.readUnsignedByte();
address = src.readUnsignedInt();
length = src.readUnsignedByte();
decimals = src.readUnsignedByte();

// skip 2:
src.position += 2;

id = src.readUnsignedByte();

// skip 2:
src.position += 2;

setFlag = src.readUnsignedByte();

// skip 7:
src.position += 7;

indexFlag = src.readUnsignedByte();
}
}

} // package
Loading

0 comments on commit e574c19

Please sign in to comment.