Search in sources :

Example 1 with AdminEntity

use of org.activityinfo.geoadmin.model.AdminEntity in project activityinfo by bedatadriven.

the class ImportWindow method doWriteImport.

private void doWriteImport() throws IOException {
    File tempFile = File.createTempFile("level", ".sql.gz");
    try (PrintWriter out = new PrintWriter(new GZIPOutputStream(new FileOutputStream(tempFile)))) {
        AdminLevel newLevel = buildUpdate();
        out.println("BEGIN;");
        out.println(String.format("INSERT INTO adminlevel (name, countryid, parentid) VALUES (%s, %d, %d);", Sql.quote(newLevel.getName()), country.getId(), parentLevel.getId()));
        out.println("select @newLevelId:=last_insert_id();");
        out.println(String.format("INSERT INTO locationtype (name, countryid, boundadminlevelid, reuse) " + "VALUES (%s, %d, @newLevelId, 0);", Sql.quote(newLevel.getName()), country.getId()));
        out.println("COMMIT;");
        out.println("BEGIN;");
        int count = 0;
        boolean first = true;
        for (AdminEntity entity : newLevel.getEntities()) {
            if (first) {
                out.println("INSERT DELAYED INTO adminentity " + "(adminlevelid, name, code, adminentityparentid, x1, y1, x2, y2) VALUES");
                first = false;
            } else {
                out.println(",");
            }
            out.print(String.format("(@newLevelId, %s, %s, %s, %f, %f, %f, %f)", Sql.quote(entity.getName()), Sql.quote(entity.getCode()), parentLevel == null ? "null" : Integer.toString(entity.getParentId()), entity.getBounds().getX1(), entity.getBounds().getY1(), entity.getBounds().getX2(), entity.getBounds().getY2()));
            count++;
            if (count % 1000 == 0) {
                out.println(";");
                first = true;
            }
        }
        out.println(";");
        out.println("COMMIT;");
    }
    System.out.println("Wrote to " + tempFile.getAbsolutePath());
}
Also used : AdminEntity(org.activityinfo.geoadmin.model.AdminEntity) GZIPOutputStream(java.util.zip.GZIPOutputStream) AdminLevel(org.activityinfo.geoadmin.model.AdminLevel)

Example 2 with AdminEntity

use of org.activityinfo.geoadmin.model.AdminEntity in project activityinfo by bedatadriven.

the class ImportWindow method buildUpdate.

private AdminLevel buildUpdate() {
    int nameAttribute = importForm.getNameAttributeIndex();
    int codeAttribute = importForm.getCodeAttributeIndex();
    List<AdminEntity> entities = Lists.newArrayList();
    Map<ImportKey, AdminEntity> entityMap = Maps.newHashMap();
    for (int i = 0; i != tableModel.getRowCount(); ++i) {
        if (tableModel.getActionAt(i) == ImportAction.IMPORT) {
            ImportFeature feature = tableModel.getFeatureAt(i);
            String featureName = feature.getAttributeStringValue(nameAttribute);
            AdminEntity parent = tableModel.getParent(i);
            if (!validateFeature(feature, featureName, parent)) {
                continue;
            }
            if (Strings.isNullOrEmpty(featureName)) {
                throw new RuntimeException("Feature " + i + " has an empty name");
            }
            // we can't have two entities with the same name within a
            // given parent. This happens often because secondary exterior rings
            // are stored as separate features.
            ImportKey key = new ImportKey(parent, featureName);
            if (!entityMap.containsKey(key)) {
                AdminEntity entity = new AdminEntity();
                String truncatedName = featureName;
                if (truncatedName.length() > MAX_NAME_LENGTH) {
                    truncatedName = truncatedName.substring(0, MAX_NAME_LENGTH);
                }
                entity.setName(truncatedName);
                if (codeAttribute != -1) {
                    entity.setCode(feature.getAttributeStringValue(codeAttribute));
                }
                Extents bounds = GeoUtils.toBounds(feature.getEnvelope());
                entity.setBounds(bounds);
                if (importForm.isGeometryImported()) {
                    entity.setGeometry(feature.getGeometry());
                }
                if (parentLevel != null) {
                    entity.setParentId(parent.getId());
                }
                entities.add(entity);
                entityMap.put(key, entity);
            } else {
                // add this geometry to the existing entity
                LOGGER.info("Merging geometry for entity named '" + featureName + "'");
                AdminEntity entity = entityMap.get(key);
                Envelope bounds = GeoUtils.toEnvelope(entity.getBounds());
                bounds.expandToInclude(feature.getEnvelope());
                entity.setBounds(GeoUtils.toBounds(bounds));
                if (importForm.isGeometryImported()) {
                    entity.setGeometry(entity.getGeometry().union(feature.getGeometry()));
                }
            }
        }
    }
    AdminLevel newLevel = new AdminLevel();
    newLevel.setName(importForm.getLevelName());
    if (parentLevel != null) {
        newLevel.setParentId(parentLevel.getId());
    }
    newLevel.setEntities(entities);
    return newLevel;
}
Also used : AdminEntity(org.activityinfo.geoadmin.model.AdminEntity) AdminLevel(org.activityinfo.geoadmin.model.AdminLevel) Extents(org.activityinfo.model.type.geo.Extents) Envelope(com.vividsolutions.jts.geom.Envelope)

Example 3 with AdminEntity

use of org.activityinfo.geoadmin.model.AdminEntity in project activityinfo by bedatadriven.

the class ImportWindow method guessParentsWithCode.

private void guessParentsWithCode() {
    int index = importForm.getParentCodeAttributeIndex();
    if (index < 0) {
        System.out.println("Choose parent code first");
        return;
    }
    Map<String, AdminEntity> codeMap = Maps.newHashMap();
    for (AdminEntity entity : this.parentEntities) {
        codeMap.put(entity.getCode(), entity);
    }
    for (int featureIndex = 0; featureIndex != source.getFeatureCount(); ++featureIndex) {
        String code = source.getFeatures().get(featureIndex).getAttributeStringValue(index);
        AdminEntity entity = codeMap.get(code);
        tableModel.setValueAt(entity, featureIndex, ImportTableModel.PARENT_COLUMN);
    }
}
Also used : AdminEntity(org.activityinfo.geoadmin.model.AdminEntity)

Example 4 with AdminEntity

use of org.activityinfo.geoadmin.model.AdminEntity in project activityinfo by bedatadriven.

the class ImportWindow method showScore.

/**
 * Display the parent match score of the selected item in the status bar
 *
 * @param featureIndex
 */
private void showScore(int featureIndex) {
    AdminEntity parent = tableModel.getParent(featureIndex);
    if (parent == null) {
        scoreLabel.setText("");
    } else {
        ImportFeature feature = tableModel.getFeatureAt(featureIndex);
        scoreLabel.setText(String.format("Scores:  Geo: %.2f  Name: %.2f  Code: %.2f", scorer.scoreGeography(feature, parent), scorer.scoreName(feature, parent), scorer.scoreCodeMatch(feature, parent)));
    }
}
Also used : AdminEntity(org.activityinfo.geoadmin.model.AdminEntity)

Example 5 with AdminEntity

use of org.activityinfo.geoadmin.model.AdminEntity in project activityinfo by bedatadriven.

the class LocationAdminMatcher method matchChildren.

private void matchChildren(AdminLevelNode parentLevel, AdminEntity parentEntity, ImportFeature feature, List<AdminEntity> matches) {
    for (AdminLevelNode level : parentLevel.getChildLevels()) {
        Integer attributeIndex = attributeMap.get(level);
        if (attributeIndex != null) {
            if (Strings.isNullOrEmpty(feature.getAttributeStringValue(attributeIndex))) {
                continue;
            }
        }
        AdminEntity bestMatch = findBestParent(feature, queryEntities(level, parentEntity));
        if (bestMatch != null) {
            matches.add(bestMatch);
            matchChildren(level, bestMatch, feature, matches);
        }
    }
}
Also used : AdminLevelNode(org.activityinfo.geoadmin.model.AdminLevelNode) AdminEntity(org.activityinfo.geoadmin.model.AdminEntity)

Aggregations

AdminEntity (org.activityinfo.geoadmin.model.AdminEntity)14 AdminLevel (org.activityinfo.geoadmin.model.AdminLevel)5 Point (com.vividsolutions.jts.geom.Point)3 Predicate (com.google.common.base.Predicate)1 Envelope (com.vividsolutions.jts.geom.Envelope)1 Component (java.awt.Component)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 Date (java.util.Date)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 ColumnGuesser (org.activityinfo.geoadmin.ColumnGuesser)1 AdminLevelNode (org.activityinfo.geoadmin.model.AdminLevelNode)1 NewLocation (org.activityinfo.geoadmin.model.NewLocation)1 Extents (org.activityinfo.model.type.geo.Extents)1