use of org.activityinfo.geoadmin.model.AdminLevel 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());
}
use of org.activityinfo.geoadmin.model.AdminLevel 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;
}
use of org.activityinfo.geoadmin.model.AdminLevel in project activityinfo by bedatadriven.
the class LocationImportWindow method doMatch.
private void doMatch() {
LocationAdminMatcher matcher = new LocationAdminMatcher(client, levels);
for (AdminLevel level : levels) {
matcher.setLevelAttribute(level, importForm.getLevelAttributeIndex(level));
}
for (LocationFeature location : locations) {
location.getEntities().clear();
for (AdminEntity entity : matcher.forFeature(location.getFeature())) {
location.getEntities().put(entity.getLevelId(), entity);
}
}
tableModel.fireTableDataChanged();
}
use of org.activityinfo.geoadmin.model.AdminLevel in project activityinfo by bedatadriven.
the class LocationImportWindow method createTableModel.
private GenericTableModel<LocationFeature> createTableModel() {
Builder<LocationFeature> model = GenericTableModel.newModel(locations);
for (final AdminLevel level : levels) {
model.addColumn(level.getName(), String.class, new Function<LocationFeature, String>() {
@Override
public String apply(LocationFeature location) {
AdminEntity entity = location.getEntities().get(level.getId());
if (entity == null) {
return null;
}
return entity.getName();
}
});
}
for (int i = 0; i != source.getAttributeCount(); ++i) {
final int attributeindex = i;
model.addColumn(source.getAttributes().get(i).getName().getLocalPart(), Object.class, new Function<LocationFeature, Object>() {
@Override
public Object apply(LocationFeature location) {
return location.getFeature().getAttributeValue(attributeindex);
}
});
}
return model.build();
}
use of org.activityinfo.geoadmin.model.AdminLevel in project activityinfo by bedatadriven.
the class ImportWindow method doImport.
protected void doImport() throws FileNotFoundException {
AdminLevel newLevel = buildUpdate();
if (parentLevel != null) {
client.postChildLevel(parentLevel, newLevel);
} else {
client.postRootLevel(country, newLevel);
}
// hide window
setVisible(false);
}
Aggregations