Search in sources :

Example 11 with ShapefileFunction

use of net.geoprism.data.importer.ShapefileFunction in project geoprism-registry by terraframe.

the class ExcelImporter method getGeometry.

@Override
public Geometry getGeometry(FeatureRow row) {
    if (this.config.isExternalImport() && this.config.getExternalSystem() instanceof RevealExternalSystem) {
        return ((RevealExcelContentHandler) this.excelHandler).getGeometry();
    } else {
        ShapefileFunction latitudeFunction = this.config.getFunction(GeoObjectImportConfiguration.LATITUDE);
        ShapefileFunction longitudeFunction = this.config.getFunction(GeoObjectImportConfiguration.LONGITUDE);
        if (latitudeFunction != null && longitudeFunction != null) {
            Object latitude = latitudeFunction.getValue(row);
            Object longitude = longitudeFunction.getValue(row);
            if (latitude != null && longitude != null) {
                Double lat = new Double(latitude.toString());
                Double lon = new Double(longitude.toString());
                if (Math.abs(lat) > 90 || Math.abs(lon) > 180) {
                    LatLonException ex = new LatLonException();
                    ex.setLat(lat.toString());
                    ex.setLon(lon.toString());
                    throw ex;
                }
                ImportConfiguration configuration = this.objectImporter.getConfiguration();
                if (configuration instanceof GeoObjectImportConfiguration) {
                    ServerGeoObjectType type = ((GeoObjectImportConfiguration) configuration).getType();
                    if (type.getGeometryType().equals(GeometryType.POINT) || type.getGeometryType().equals(GeometryType.MIXED)) {
                        return new Point(new CoordinateSequence2D(lon, lat), factory);
                    }
                }
                return new MultiPoint(new Point[] { new Point(new CoordinateSequence2D(lon, lat), factory) }, factory);
            }
        }
        return null;
    }
}
Also used : LatLonException(net.geoprism.registry.io.LatLonException) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) GeoObjectImportConfiguration(net.geoprism.registry.io.GeoObjectImportConfiguration) CoordinateSequence2D(org.jaitools.jts.CoordinateSequence2D) RevealExternalSystem(net.geoprism.registry.graph.RevealExternalSystem) ServerGeoObjectType(net.geoprism.registry.model.ServerGeoObjectType) ShapefileFunction(net.geoprism.data.importer.ShapefileFunction) GeoObjectImportConfiguration(net.geoprism.registry.io.GeoObjectImportConfiguration) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

Example 12 with ShapefileFunction

use of net.geoprism.data.importer.ShapefileFunction in project geoprism-registry by terraframe.

the class GeoObjectImporter method importRowInTrans.

@Transaction
public void importRowInTrans(FeatureRow row, RowData data) {
    // Refresh the session because it might expire on long imports
    final long curWorkProgress = this.progressListener.getWorkProgress();
    if ((this.lastImportSessionRefresh + GeoObjectImporter.refreshSessionRecordCount) < curWorkProgress) {
        SessionFacade.renewSession(Session.getCurrentSession().getOid());
        this.lastImportSessionRefresh = curWorkProgress;
    }
    GeoObjectOverTime go = null;
    String goJson = null;
    ServerGeoObjectIF serverGo = null;
    ServerGeoObjectIF parent = null;
    boolean isNew = false;
    GeoObjectParentErrorBuilder parentBuilder = new GeoObjectParentErrorBuilder();
    try {
        String code = this.getCode(row);
        if (code == null || code.length() <= 0) {
            RequiredMappingException ex = new RequiredMappingException();
            ex.setAttributeLabel(GeoObjectTypeMetadata.getAttributeDisplayLabel(DefaultAttribute.CODE.getName()));
            throw ex;
        }
        if (this.configuration.getImportStrategy().equals(ImportStrategy.UPDATE_ONLY) || this.configuration.getImportStrategy().equals(ImportStrategy.NEW_AND_UPDATE)) {
            serverGo = service.getGeoObjectByCode(code, this.configuration.getType(), false);
        }
        if (serverGo == null) {
            if (this.configuration.getImportStrategy().equals(ImportStrategy.UPDATE_ONLY)) {
                net.geoprism.registry.DataNotFoundException ex = new net.geoprism.registry.DataNotFoundException();
                ex.setTypeLabel(GeoObjectMetadata.get().getClassDisplayLabel());
                ex.setDataIdentifier(code);
                ex.setAttributeLabel(GeoObjectMetadata.get().getAttributeDisplayLabel(DefaultAttribute.CODE.getName()));
                throw ex;
            }
            isNew = true;
            serverGo = service.newInstance(this.configuration.getType());
            serverGo.setCode(code);
            serverGo.setInvalid(false);
        } else {
            serverGo.lock();
        }
        try {
            LocalizedValue entityName = this.getName(row);
            if (entityName != null && this.hasValue(entityName)) {
                serverGo.setDisplayLabel(entityName, this.configuration.getStartDate(), this.configuration.getEndDate());
            }
            Geometry geometry = (Geometry) this.getFormatSpecificImporter().getGeometry(row);
            if (geometry != null) {
                // geometry.getSRID().
                if (geometry.isValid()) {
                    serverGo.setGeometry(geometry, this.configuration.getStartDate(), this.configuration.getEndDate());
                } else {
                    // throw new SridException();
                    throw new InvalidGeometryException();
                }
            }
            if (isNew) {
                serverGo.setUid(ServiceFactory.getIdService().getUids(1)[0]);
            }
            // Set exists first so we can validate attributes on it
            // ShapefileFunction existsFunction = this.configuration.getFunction(DefaultAttribute.EXISTS.getName());
            // 
            // if (existsFunction != null)
            // {
            // Object value = existsFunction.getValue(row);
            // 
            // if (value != null && !this.isEmptyString(value))
            // {
            // this.setValue(serverGo, this.configuration.getType().getAttribute(DefaultAttribute.EXISTS.getName()).get(), DefaultAttribute.EXISTS.getName(), value);
            // }
            // }
            // else if (isNew)
            // {
            // ValueOverTime defaultExists = ((VertexServerGeoObject) serverGo).buildDefaultExists();
            // if (defaultExists != null)
            // {
            // serverGo.setValue(DefaultAttribute.EXISTS.getName(), Boolean.TRUE, defaultExists.getStartDate(), defaultExists.getEndDate());
            // }
            // }
            this.setValue(serverGo, this.configuration.getType().getAttribute(DefaultAttribute.EXISTS.getName()).get(), DefaultAttribute.EXISTS.getName(), true);
            Map<String, AttributeType> attributes = this.configuration.getType().getAttributeMap();
            Set<Entry<String, AttributeType>> entries = attributes.entrySet();
            for (Entry<String, AttributeType> entry : entries) {
                String attributeName = entry.getKey();
                if (!attributeName.equals(GeoObject.CODE) && !attributeName.equals(DefaultAttribute.EXISTS.getName())) {
                    ShapefileFunction function = this.configuration.getFunction(attributeName);
                    if (function != null) {
                        Object value = function.getValue(row);
                        AttributeType attributeType = entry.getValue();
                        if (value != null && !this.isEmptyString(value)) {
                            // if (!(existsFunction == null && isNew))
                            // {
                            // try
                            // {
                            // ((VertexServerGeoObject) serverGo).enforceAttributeSetWithinRange(serverGo.getDisplayLabel().getValue(), attributeName, this.configuration.getStartDate(), this.configuration.getEndDate());
                            // }
                            // catch (ValueOutOfRangeException e)
                            // {
                            // final SimpleDateFormat format = ValueOverTimeDTO.getTimeFormatter();
                            // 
                            // ImportOutOfRangeException ex = new ImportOutOfRangeException();
                            // ex.setStartDate(format.format(this.configuration.getStartDate()));
                            // 
                            // if (ValueOverTime.INFINITY_END_DATE.equals(this.configuration.getEndDate()))
                            // {
                            // ex.setEndDate(LocalizationFacade.localize("changeovertime.present"));
                            // }
                            // else
                            // {
                            // ex.setEndDate(format.format(this.configuration.getEndDate()));
                            // }
                            // 
                            // throw ex;
                            // }
                            // }
                            this.setValue(serverGo, attributeType, attributeName, value);
                        } else if (this.configuration.getCopyBlank()) {
                            this.setValue(serverGo, attributeType, attributeName, null);
                        }
                    }
                }
            }
            go = serverGo.toGeoObjectOverTime(false);
            goJson = go.toJSON().toString();
            /*
         * Try to get the parent and ensure that this row is not ignored. The
         * getParent method will throw a IgnoreRowException if the parent is
         * configured to be ignored.
         */
            if (this.configuration.isPostalCode() && PostalCodeFactory.isAvailable(this.configuration.getType())) {
                parent = this.parsePostalCode(row);
            } else if (this.configuration.getHierarchy() != null && this.configuration.getLocations().size() > 0) {
                parent = this.getParent(row);
            }
            parentBuilder.setParent(parent);
            if (this.progressListener.hasValidationProblems()) {
                throw new RuntimeException("Did not expect to encounter validation problems during import.");
            }
            data.setGoJson(goJson);
            data.setNew(isNew);
            data.setParentBuilder(parentBuilder);
            serverGo.apply(true);
        } finally {
            if (serverGo != null) {
                serverGo.unlock();
            }
        }
        if (this.configuration.isExternalImport()) {
            ShapefileFunction function = this.configuration.getExternalIdFunction();
            Object value = function.getValue(row);
            serverGo.createExternalId(this.configuration.getExternalSystem(), String.valueOf(value), this.configuration.getImportStrategy());
        }
        if (parent != null) {
            parent.addChild(serverGo, this.configuration.getHierarchy(), this.configuration.getStartDate(), this.configuration.getEndDate());
        } else if (isNew) {
        // GeoEntity child = GeoEntity.getByKey(serverGo.getCode());
        // GeoEntity root = GeoEntity.getByKey(GeoEntity.ROOT);
        // 
        // child.addLink(root,
        // this.configuration.getHierarchy().getEntityType());
        }
        // We must ensure that any problems created during the transaction are
        // logged now instead of when the request returns. As such, if any
        // problems exist immediately throw a ProblemException so that normal
        // exception handling can occur.
        List<ProblemIF> problems = RequestState.getProblemsInCurrentRequest();
        List<ProblemIF> problems2 = new LinkedList<ProblemIF>();
        for (ProblemIF problem : problems) {
            problems2.add(problem);
        }
        if (problems.size() != 0) {
            throw new ProblemException(null, problems2);
        }
        this.progressListener.setImportedRecords(this.progressListener.getImportedRecords() + 1);
    } catch (IgnoreRowException e) {
    // Do nothing
    } catch (Throwable t) {
        buildRecordException(goJson, isNew, parentBuilder, t);
    }
    this.progressListener.setWorkProgress(curWorkProgress + 1);
}
Also used : ProblemException(com.runwaysdk.ProblemException) ProblemIF(com.runwaysdk.ProblemIF) ServerGeoObjectIF(net.geoprism.registry.model.ServerGeoObjectIF) GeoObjectOverTime(org.commongeoregistry.adapter.dataaccess.GeoObjectOverTime) IgnoreRowException(net.geoprism.registry.io.IgnoreRowException) Entry(java.util.Map.Entry) LocalizedValue(org.commongeoregistry.adapter.dataaccess.LocalizedValue) AttributeType(org.commongeoregistry.adapter.metadata.AttributeType) ShapefileFunction(net.geoprism.data.importer.ShapefileFunction) InvalidGeometryException(net.geoprism.registry.io.InvalidGeometryException) LinkedList(java.util.LinkedList) Geometry(com.vividsolutions.jts.geom.Geometry) RequiredMappingException(net.geoprism.registry.io.RequiredMappingException) GeoObject(org.commongeoregistry.adapter.dataaccess.GeoObject) JSONObject(org.json.JSONObject) VertexServerGeoObject(net.geoprism.registry.model.graph.VertexServerGeoObject) VertexObject(com.runwaysdk.business.graph.VertexObject) Transaction(com.runwaysdk.dataaccess.transaction.Transaction)

Example 13 with ShapefileFunction

use of net.geoprism.data.importer.ShapefileFunction in project geoprism-registry by terraframe.

the class ShapefileImporter method process.

/**
 * Imports the entities from the shapefile
 *
 * @param writer
 *          Log file writer
 * @throws InvocationTargetException
 * @throws IOException
 * @throws InterruptedException
 */
@Request
private void process(ImportStage stage, File shp) throws InvocationTargetException, IOException, InterruptedException {
    /*
     * Check permissions
     */
    ImportConfiguration config = this.getObjectImporter().getConfiguration();
    config.enforceCreatePermissions();
    FileDataStore myData = FileDataStoreFinder.getDataStore(shp);
    SimpleFeatureSource source = myData.getFeatureSource();
    SimpleFeatureCollection featCol = source.getFeatures();
    this.progressListener.setWorkTotal((long) featCol.size());
    if (this.getStartIndex() > 0) {
        Query query = new Query();
        query.setStartIndex(Math.toIntExact(this.getStartIndex()));
        featCol = source.getFeatures(query);
    }
    SimpleFeatureIterator featIt = featCol.features();
    SimpleFeatureReader fr = new DelegateSimpleFeatureReader(source.getSchema(), featIt);
    FilterFactory ff = CommonFactoryFinder.getFilterFactory(null);
    // We want to sort the features by the parentId column for better lookup
    // performance (more cache hits) and
    // also so that we have predictable ordering if we want to resume the import
    // later.
    List<SortBy> sortBy = new ArrayList<SortBy>();
    // We also sort by featureId because it's
    sortBy.add(SortBy.NATURAL_ORDER);
    // guaranteed to be unique.
    if (this.config.getLocations().size() > 0) {
        ShapefileFunction loc = this.config.getLocations().get(0).getFunction();
        if (loc instanceof BasicColumnFunction) {
            // TODO
            sortBy.add(ff.sort(loc.toJson().toString(), SortOrder.ASCENDING));
        // :
        // This
        // assumes
        // loc.tojson()
        // returns
        // only
        // the
        // attribute
        // name.
        }
    }
    try (SimpleFeatureReader sr = new SortedFeatureReader(fr, sortBy.toArray(new SortBy[sortBy.size()]), 5000)) {
        while (sr.hasNext()) {
            SimpleFeature feature = sr.next();
            if (stage.equals(ImportStage.VALIDATE)) {
                this.objectImporter.validateRow(new SimpleFeatureRow(feature));
            } else {
                this.objectImporter.importRow(new SimpleFeatureRow(feature));
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
    } finally {
        myData.dispose();
    }
}
Also used : Query(org.geotools.data.Query) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) SortBy(org.opengis.filter.sort.SortBy) BasicColumnFunction(net.geoprism.data.importer.BasicColumnFunction) ArrayList(java.util.ArrayList) SimpleFeatureReader(org.geotools.data.simple.SimpleFeatureReader) DelegateSimpleFeatureReader(org.geotools.data.simple.DelegateSimpleFeatureReader) SortedFeatureReader(org.geotools.data.sort.SortedFeatureReader) FilterFactory(org.opengis.filter.FilterFactory) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) ShapefileFunction(net.geoprism.data.importer.ShapefileFunction) SimpleFeatureRow(net.geoprism.data.importer.SimpleFeatureRow) FileDataStore(org.geotools.data.FileDataStore) DelegateSimpleFeatureReader(org.geotools.data.simple.DelegateSimpleFeatureReader) Request(com.runwaysdk.session.Request)

Example 14 with ShapefileFunction

use of net.geoprism.data.importer.ShapefileFunction in project geoprism-registry by terraframe.

the class GeoObjectImportConfiguration method toJSON.

@Request
@Override
public JSONObject toJSON() {
    JSONObject config = new JSONObject();
    super.toJSON(config);
    SimpleDateFormat format = new SimpleDateFormat(GeoObjectImportConfiguration.DATE_FORMAT);
    format.setTimeZone(GeoRegistryUtil.SYSTEM_TIMEZONE);
    JSONObject type = new JSONObject(this.type.toJSON(new ImportAttributeSerializer(Session.getCurrentLocale(), this.includeCoordinates, false, LocalizationFacade.getInstalledLocales())).toString());
    JSONArray attributes = type.getJSONArray(GeoObjectType.JSON_ATTRIBUTES);
    for (int i = 0; i < attributes.length(); i++) {
        JSONObject attribute = attributes.getJSONObject(i);
        String attributeName = attribute.getString(AttributeType.JSON_CODE);
        if (this.functions.containsKey(attributeName)) {
            ShapefileFunction function = this.functions.get(attributeName);
            if (function instanceof LocalizedValueFunction) {
                String locale = attribute.getString("locale");
                ShapefileFunction localeFunction = ((LocalizedValueFunction) function).getFunction(locale);
                if (localeFunction != null) {
                    attribute.put(TARGET, localeFunction.toJson());
                }
            } else {
                attribute.put(TARGET, function.toJson());
            }
        }
    }
    JSONArray locations = new JSONArray();
    for (Location location : this.locations) {
        locations.put(location.toJSON());
    }
    config.put(GeoObjectImportConfiguration.TYPE, type);
    config.put(GeoObjectImportConfiguration.LOCATIONS, locations);
    config.put(GeoObjectImportConfiguration.POSTAL_CODE, this.isPostalCode());
    if (this.getStartDate() != null) {
        config.put(GeoObjectImportConfiguration.START_DATE, format.format(this.getStartDate()));
    }
    if (this.getEndDate() != null) {
        config.put(GeoObjectImportConfiguration.END_DATE, format.format(this.getEndDate()));
    }
    if (this.hierarchy != null) {
        config.put(GeoObjectImportConfiguration.HIERARCHY, this.getHierarchy().getCode());
    }
    if (this.exclusions.size() > 0) {
        JSONArray exclusions = new JSONArray();
        this.exclusions.forEach((key, set) -> {
            set.forEach(value -> {
                JSONObject object = new JSONObject();
                object.put(AttributeType.JSON_CODE, key);
                object.put(VALUE, value);
                exclusions.put(object);
            });
        });
        config.put(EXCLUSIONS, exclusions);
    }
    if (this.getRevealGeometryColumn() != null) {
        config.put(REVEAL_GEOMETRY_COLUMN, revealGeometryColumn);
    }
    return config;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ShapefileFunction(net.geoprism.data.importer.ShapefileFunction) SimpleDateFormat(java.text.SimpleDateFormat) Request(com.runwaysdk.session.Request)

Aggregations

ShapefileFunction (net.geoprism.data.importer.ShapefileFunction)14 JSONObject (org.json.JSONObject)10 VertexObject (com.runwaysdk.business.graph.VertexObject)7 GeoObject (org.commongeoregistry.adapter.dataaccess.GeoObject)7 RequiredMappingException (net.geoprism.registry.io.RequiredMappingException)6 Entry (java.util.Map.Entry)5 Transaction (com.runwaysdk.dataaccess.transaction.Transaction)4 Request (com.runwaysdk.session.Request)4 IgnoreRowException (net.geoprism.registry.io.IgnoreRowException)4 ServerGeoObjectIF (net.geoprism.registry.model.ServerGeoObjectIF)4 VertexServerGeoObject (net.geoprism.registry.model.graph.VertexServerGeoObject)4 LocalizedValue (org.commongeoregistry.adapter.dataaccess.LocalizedValue)4 AttributeType (org.commongeoregistry.adapter.metadata.AttributeType)4 Location (net.geoprism.registry.io.Location)3 BusinessObject (net.geoprism.registry.model.BusinessObject)3 ProblemException (com.runwaysdk.ProblemException)2 ProblemIF (com.runwaysdk.ProblemIF)2 Geometry (com.vividsolutions.jts.geom.Geometry)2 SimpleDateFormat (java.text.SimpleDateFormat)2 RowValidationProblem (net.geoprism.registry.etl.RowValidationProblem)2