88229aebf9c67e67fe392463d4f02aea1c970e53
[potlatch2.git] / net / systemeD / potlatch2 / utils / ShpImporter.as
1 package net.systemeD.potlatch2.utils {
2
3         import net.systemeD.halcyon.Map;
4         import net.systemeD.halcyon.connection.*;
5         import org.vanrijkom.shp.*;
6         import org.vanrijkom.dbf.*;
7
8         import net.systemeD.halcyon.Globals;
9
10         // SHP class docs and examples:
11         //              http://vanrijkom.org/shp/index.html
12         //              http://www.boxshapedworld.com/blog/post/Shapefiles-Actionscript-30-and-Google-Maps.aspx
13         //              http://web.archive.org/web/20071119113250rn_1/vanrijkom.org/samples/fsd-mexico/srcview/
14         // we load .shp as files[0], .shx as files[1], .dbf as files[2]
15
16         // See http://www.actionscript.org/forums/showthread.php3?t=185320 for tips on avoiding time-outs with big files -
17         // probably needs to be asynchronous
18                 
19         public class ShpImporter extends Importer  {
20
21                 public function ShpImporter(map:Map, filenames:Array) {
22                         super(map, filenames);
23                 }
24                 
25                 // All data is loaded, so do the import
26
27                 override protected function doImport():void {
28                         Globals.vars.root.addDebug("importing");
29                         var shp:ShpHeader=new ShpHeader(files[0]);
30                         var dbf:DbfHeader=new DbfHeader(files[2]);
31
32                         if (shp.shapeType==ShpType.SHAPE_POLYGON || shp.shapeType==ShpType.SHAPE_POLYLINE) {
33
34                                 // Loop through all polylines in the shape
35                                 var polyArray:Array = ShpTools.readRecords(files[0]);
36                                 for (var i:uint=0; i<Math.min(polyArray.length,50); i++) {
37
38                                         // Get attributes like this:
39                                         //              var dr:DbfRecord = DbfTools.getRecord(files[2], dbf, i);
40                                         //              var xsID:String = dr.values[idFieldName];
41
42                                         // Do each ring in turn, then each point in the ring
43                                         for (var j:int=0; j < Math.min(polyArray[i].shape.rings.length,50); j++) {
44                                                 var nodes:Array=[];
45                                                 var points:Array = polyArray[i].shape.rings[j];
46                                                 if (points!=null) {
47                                                         for (var k:int=0; k < Math.min(points.length,50); k++) {
48                                                                 var p:ShpPoint = ShpPoint(points[k]);
49                                                 var node:Node = map.connection.createNode({}, p.y, p.x);
50                                                                 nodes.push(node);
51                                                                 Globals.vars.root.addDebug("point "+p.x+","+p.y);
52                                                         }
53                                                 }
54                                                 if (nodes.length>0) { var way:Way = map.connection.createWay({}, nodes); }
55                                         }
56                                 }
57                         }
58                 }
59         }
60 }