Chris Mutel avatar Chris Mutel committed f3769e6

Fixes to get current version to work

Comments (0)

Files changed (6)

py_amoeba/amoeba.py

                         loop_score < 0):
                     break
                 else:
-                    new_neighbors = set([int(x) for x in data[:index,0]])
+                    new_neighbors = set(unique(data[:index, 0].astype(int) 
+                        ).tolist())
                     loop_score = new_score
 
             if loop_score == base_score:
 
     def __init__(self, *args, **kwargs):
         super(ShapefileAMOEBA, self).__init__(*args, **kwargs)
-        self.create_neighbor_cache()
+        self.neighbor_cache = {}
 
     def get_ds(self, ds):
         if isinstance(ds, basestring):
         else:
             raise TypeError("Data source not understood")
 
-    def create_neighbor_cache(self):
-        self.neighbor_cache = {}
-        for feat in self.ds:
-            self.neighbor_cache[self.get_oid(feat)] = self.ds.intersects( 
-                self.get_buffered_geom(feat), excluded=(self.get_oid(feat),))
+    def get_neighbors(self, n):
+        try:
+            return self.neighbor_cache[n]
+        except KeyError:
+            feat = self.ds.get_feature_by_id(n)
+            neighbors = self.ds.intersects(self.get_buffered_geom(feat), 
+                excluded=(n,))
+            self.neighbor_cache[n] = neighbors
+            return neighbors
 
     def expand(self, tiers, excluded):
-        if tiers.level > 10:
+        if tiers.level > 6:
             # One could continue to expand, but there is not really any 
-            # point; the spatial weights are < 1e-6 anyway.
+            # point; the spatial weights are quite small anyway.
             return []
         new_neighbors = reduce(
             set.union,
-            [set(self.neighbor_cache[x]) for x in tiers.geo[tiers.level]]
+            [set(self.get_neighbors(x)) for x in tiers.geo[tiers.level]]
             )
         return list(new_neighbors.difference(excluded).difference( 
             tiers.all_geo))

py_amoeba/geodjango_amoeba.py

 from geodjango_shapefile import GeodjangoShapefileReader
 
 
-class DjangoShapefileAMOEBA(ShapefileAMOEBA):
+class GeodjangoShapefileAMOEBA(ShapefileAMOEBA):
     """Use Django bindings instead of OGR bindings, as they don't segfault every 10 seconds."""
     shapefile_reader = GeodjangoShapefileReader
 

py_amoeba/geodjango_shapefile.py

 class GeodjangoShapefileReader(ShapefileReader):
     """Geodjango wrapper around a shapefile."""
     def open_datasource(self, filepath, layer):
-        ds = DataSource(filepath)
+        self.filepath = filepath
+        ds = DataSource(self.filepath)
         return ds, ds[layer]
 
     def default_fieldname(self):
     def get_feature_tuple(self, feat):
         return (feat.fid, feat.geom.envelope.tuple)
 
+    def get_feature_by_id(self, i):
+        # Can't assume that feature IDs are contiguous
+        # if not hasattr(self, "feature_id_dict"):
+        #     self.feature_id_dict = dict([(f.fid, i) for i, f in \
+        #         enumerate(self.layer)])
+        # return self.layer[self.feature_id_dict[i]]
+        return self.layer[i]
+
     def reset_layer(self):
         pass
 
 
     def get_field_tuple(self, f):
         return (f.fid, float(f.get(self.amount_field)))
+
+    def __iter__(self):
+        return self.layer.__iter__()
+

py_amoeba/shapefile.py

     def reset_layer(self):
         self.layer.ResetReading()
 
+    def get_feature_by_id(self, i):
+        return self.layer.GetFeature(i)
+
     def create_spatial_index(self):
         """Create and populate r-tree spatial index."""
         self.index = index.Index()

py_amoeba/tier_dictionary.py

 
     def add(self, objs, mapping, score):
         if set(objs).intersection(self.all_geo):
-            print set(objs).intersection(self.all_geo)
+            # print set(objs).intersection(self.all_geo)
             raise ValueError
         self.level += 1
         self.geo[self.level] = set(objs)
 numpy
 scipy
-rtree
+Rtree
 osgeo
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.