Search in sources :

Example 6 with TLongArrayList

use of gnu.trove.list.array.TLongArrayList in project OsmAnd-tools by osmandapp.

the class OsmDbCreator method acceptEntityToLoad.

@Override
public boolean acceptEntityToLoad(OsmBaseStorage storage, EntityId entityId, Entity e) {
    // put all nodes into temporary db to get only required nodes after loading all data
    if (VALIDATE_DUPLICATES) {
        long l = (e.getId() << 2) + entityId.getType().ordinal();
        if (!idSet.add(l)) {
            throw new IllegalStateException("Duplicate id '" + e.getId() + "' " + entityId.getType());
        }
    }
    try {
        e.removeTags(tagsToIgnore);
        ByteArrayOutputStream tags = new ByteArrayOutputStream();
        try {
            for (Entry<String, String> i : e.getTags().entrySet()) {
                // UTF-8 default
                tags.write(i.getKey().getBytes("UTF-8"));
                tags.write(0);
                tags.write(i.getValue().getBytes("UTF-8"));
                tags.write(0);
            }
        } catch (IOException es) {
            throw new RuntimeException(es);
        }
        long id = convertId(e);
        boolean delete = AugmentedDiffsInspector.OSMAND_DELETE_VALUE.equals(e.getTag(AugmentedDiffsInspector.OSMAND_DELETE_TAG));
        if (e.getTags().isEmpty()) {
            e.putTag(AugmentedDiffsInspector.OSMAND_DELETE_TAG, AugmentedDiffsInspector.OSMAND_DELETE_VALUE);
            delete = true;
        }
        if (ovewriteIds || e instanceof Relation) {
            checkEntityExists(e, id, delete);
        }
        if (e instanceof Node) {
            currentCountNode++;
            if (!e.getTags().isEmpty()) {
                allNodes++;
            }
            prepNode.setLong(1, id);
            prepNode.setDouble(2, ((Node) e).getLatitude());
            prepNode.setDouble(3, ((Node) e).getLongitude());
            prepNode.setBytes(4, tags.toByteArray());
            prepNode.addBatch();
            if (currentCountNode >= BATCH_SIZE_OSM) {
                prepNode.executeBatch();
                // clear memory
                dbConn.commit();
                currentCountNode = 0;
            }
        } else if (e instanceof Way) {
            allWays++;
            int ord = 0;
            TLongArrayList nodeIds = ((Way) e).getNodeIds();
            boolean city = CityType.valueFromString(((Way) e).getTag(OSMTagKey.PLACE)) != null;
            int boundary = ((Way) e).getTag(OSMTagKey.BOUNDARY) != null || city ? 1 : 0;
            for (int j = 0; j < nodeIds.size(); j++) {
                currentWaysCount++;
                if (ord == 0) {
                    prepWays.setBytes(4, tags.toByteArray());
                }
                prepWays.setLong(1, id);
                prepWays.setLong(2, nodeIds.get(j));
                prepWays.setLong(3, ord++);
                prepWays.setInt(5, boundary);
                prepWays.addBatch();
            }
            if (currentWaysCount >= BATCH_SIZE_OSM) {
                prepWays.executeBatch();
                // clear memory
                dbConn.commit();
                currentWaysCount = 0;
            }
        } else {
            // osm change can't handle relations properly
            allRelations++;
            short ord = 0;
            for (RelationMember i : ((Relation) e).getMembers()) {
                currentRelationsCount++;
                if (ord == 0) {
                    prepRelations.setBytes(6, tags.toByteArray());
                }
                prepRelations.setLong(1, id);
                prepRelations.setLong(2, i.getEntityId().getId());
                prepRelations.setLong(3, i.getEntityId().getType().ordinal());
                prepRelations.setString(4, i.getRole());
                prepRelations.setLong(5, ord++);
                prepRelations.setInt(7, delete ? 1 : 0);
                prepRelations.addBatch();
            }
            // System.out.println(id + " " + delete);
            if (currentRelationsCount >= BATCH_SIZE_OSM) {
                prepRelations.executeBatch();
                // clear memory
                dbConn.commit();
                currentRelationsCount = 0;
            }
        }
    } catch (SQLException ex) {
        // $NON-NLS-1$
        log.error("Could not save in db (entity " + entityId + ") ", ex);
    }
    // do not add to storage
    return false;
}
Also used : TLongArrayList(gnu.trove.list.array.TLongArrayList) SQLException(java.sql.SQLException) Node(net.osmand.osm.edit.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Way(net.osmand.osm.edit.Way) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember)

Example 7 with TLongArrayList

use of gnu.trove.list.array.TLongArrayList in project spf4j by zolyfarkas.

the class TimeSeriesDatabase method read.

/**
 * Read measurements from table.
 *
 * @param tableName
 * @param startTime start time including
 * @param endTime end time including
 * @return
 * @throws IOException
 */
private TimeSeries read(final long startTime, final long endTime, final long startAtFragment, final long endAtFragment, final boolean skipFirst) throws IOException {
    synchronized (path) {
        TLongArrayList timeStamps = new TLongArrayList();
        List<long[]> data = new ArrayList<>();
        if (startAtFragment > 0) {
            FileLock lock = ch.lock(0, Long.MAX_VALUE, true);
            try {
                DataFragment frag;
                long nextFragmentLocation = startAtFragment;
                boolean last = false;
                boolean psFirst = skipFirst;
                do {
                    if (nextFragmentLocation == endAtFragment) {
                        last = true;
                    }
                    file.seek(nextFragmentLocation);
                    frag = new DataFragment(file);
                    if (psFirst) {
                        psFirst = false;
                    } else {
                        long fragStartTime = frag.getStartTimeMillis();
                        if (fragStartTime >= startTime) {
                            TIntArrayList fragTimestamps = frag.getTimestamps();
                            int nr = 0;
                            for (int i = 0; i < fragTimestamps.size(); i++) {
                                long ts = fragStartTime + fragTimestamps.get(i);
                                if (ts <= endTime) {
                                    timeStamps.add(ts);
                                    nr++;
                                } else {
                                    break;
                                }
                            }
                            int i = 0;
                            for (long[] d : frag.getData()) {
                                if (i < nr) {
                                    data.add(d);
                                } else {
                                    break;
                                }
                                nr++;
                            }
                            if (fragTimestamps.size() > nr) {
                                break;
                            }
                        }
                    }
                    nextFragmentLocation = frag.getNextDataFragment();
                } while (nextFragmentLocation > 0 && !last);
            } catch (IOException | RuntimeException e) {
                try {
                    lock.release();
                    throw e;
                } catch (IOException ex) {
                    ex.addSuppressed(e);
                    throw ex;
                }
            }
            lock.release();
        }
        return new TimeSeries(timeStamps.toArray(), data.toArray(new long[data.size()][]));
    }
}
Also used : TimeSeries(org.spf4j.tsdb2.TimeSeries) TLongArrayList(gnu.trove.list.array.TLongArrayList) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) IOException(java.io.IOException) TIntArrayList(gnu.trove.list.array.TIntArrayList) FileLock(java.nio.channels.FileLock)

Example 8 with TLongArrayList

use of gnu.trove.list.array.TLongArrayList in project Osmand by osmandapp.

the class BinaryInspector method printOsmRouteDetails.

private void printOsmRouteDetails(RouteDataObject obj, StringBuilder b) {
    StringBuilder tags = new StringBuilder();
    int[] types = obj.getTypes();
    for (int j = 0; j < types.length; j++) {
        RouteTypeRule rt = obj.region.quickGetEncodingRule(types[j]);
        if (rt == null) {
            throw new NullPointerException("Type " + types[j] + "was not found");
        }
        String value = quoteName(rt.getValue());
        tags.append("\t<tag k='").append(rt.getTag()).append("' v='").append(value).append("' />\n");
    }
    TIntObjectHashMap<String> names = obj.getNames();
    if (names != null && !names.isEmpty()) {
        int[] keys = names.keys();
        for (int j = 0; j < keys.length; j++) {
            RouteTypeRule rt = obj.region.quickGetEncodingRule(keys[j]);
            if (rt == null) {
                throw new NullPointerException("Type " + keys[j] + "was not found");
            }
            String name = quoteName(names.get(keys[j]));
            tags.append("\t<tag k='").append(rt.getTag()).append("' v='").append(name).append("' />\n");
        }
    }
    tags.append("\t<tag k=\'").append("original_id").append("' v='").append(obj.getId() >> (SHIFT_ID)).append("'/>\n");
    tags.append("\t<tag k=\'").append("osmand_id").append("' v='").append(obj.getId()).append("'/>\n");
    TLongArrayList ids = new TLongArrayList();
    for (int i = 0; i < obj.getPointsLength(); i++) {
        float lon = (float) MapUtils.get31LongitudeX(obj.getPoint31XTile(i));
        float lat = (float) MapUtils.get31LatitudeY(obj.getPoint31YTile(i));
        int id = OSM_ID++;
        b.append("\t<node id = '" + id + "' version='1' lat='" + lat + "' lon='" + lon + "' >\n");
        if (obj.getPointNames(i) != null) {
            String[] vs = obj.getPointNames(i);
            int[] keys = obj.getPointNameTypes(i);
            for (int j = 0; j < keys.length; j++) {
                RouteTypeRule rt = obj.region.quickGetEncodingRule(keys[j]);
                String name = quoteName(vs[j]);
                tags.append("\t\t<tag k='").append(rt.getTag()).append("' v='").append(name).append("' />\n");
            }
        }
        if (obj.getPointTypes(i) != null) {
            int[] keys = obj.getPointTypes(i);
            for (int j = 0; j < keys.length; j++) {
                RouteTypeRule rt = obj.region.quickGetEncodingRule(keys[j]);
                String value = quoteName(rt.getValue());
                tags.append("\t\t<tag k='").append(rt.getTag()).append("' v='").append(value).append("' />\n");
            }
        }
        b.append("\t</node >\n");
        ids.add(id);
    }
    long idway = printWay(ids, b, tags);
    if (obj.getRestrictionLength() > 0) {
        for (int i = 0; i < obj.getRestrictionLength(); i++) {
            long ld = obj.getRestrictionId(i);
            String tp = MapRenderingTypes.getRestrictionValue(obj.getRestrictionType(i));
            int id = OSM_ID++;
            b.append("<relation id = '" + id + "' version='1'>\n");
            b.append("\t<member ref='").append(idway).append("' role='from' type='way' />\n");
            b.append("\t<tag k='").append("from_osmand_id").append("' v='").append(obj.getId()).append("' />\n");
            b.append("\t<tag k='").append("from_id").append("' v='").append(obj.getId() >> SHIFT_ID).append("' />\n");
            b.append("\t<tag k='").append("to_osmand_id").append("' v='").append(ld).append("' />\n");
            b.append("\t<tag k='").append("to_id").append("' v='").append(ld >> SHIFT_ID).append("' />\n");
            b.append("\t<tag k='").append("type").append("' v='").append("restriction").append("' />\n");
            b.append("\t<tag k='").append("restriction").append("' v='").append(tp).append("' />\n");
            b.append("</relation>\n");
        }
    }
}
Also used : TLongArrayList(gnu.trove.list.array.TLongArrayList) RouteTypeRule(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteTypeRule)

Example 9 with TLongArrayList

use of gnu.trove.list.array.TLongArrayList in project Osmand by osmandapp.

the class BinaryMapRouteReaderAdapter method loadRouteRegionData.

public List<RouteDataObject> loadRouteRegionData(RouteSubregion rs) throws IOException {
    TLongArrayList idMap = new TLongArrayList();
    TLongObjectHashMap<TLongArrayList> restrictionMap = new TLongObjectHashMap<TLongArrayList>();
    if (rs.dataObjects == null) {
        codedIS.seek(rs.filePointer + rs.shiftToData);
        int limit = codedIS.readRawVarint32();
        int oldLimit = codedIS.pushLimit(limit);
        readRouteTreeData(rs, idMap, restrictionMap);
        codedIS.popLimit(oldLimit);
    }
    List<RouteDataObject> res = rs.dataObjects;
    rs.dataObjects = null;
    return res;
}
Also used : TLongArrayList(gnu.trove.list.array.TLongArrayList) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap)

Example 10 with TLongArrayList

use of gnu.trove.list.array.TLongArrayList in project Osmand by osmandapp.

the class Way method addNode.

public void addNode(Node n) {
    if (nodeIds == null) {
        nodeIds = new TLongArrayList();
    }
    if (nodes == null) {
        nodes = new ArrayList<Node>();
    }
    nodeIds.add(n.getId());
    nodes.add(n);
}
Also used : TLongArrayList(gnu.trove.list.array.TLongArrayList)

Aggregations

TLongArrayList (gnu.trove.list.array.TLongArrayList)28 ArrayList (java.util.ArrayList)5 TIntArrayList (gnu.trove.list.array.TIntArrayList)4 Test (org.junit.Test)4 CandidateCellForSweeping (com.palantir.atlasdb.keyvalue.api.CandidateCellForSweeping)3 ImmutableCandidateCellForSweeping (com.palantir.atlasdb.keyvalue.api.ImmutableCandidateCellForSweeping)3 TLongList (gnu.trove.list.TLongList)3 Relation (net.osmand.osm.edit.Relation)3 RelationMember (net.osmand.osm.edit.Relation.RelationMember)3 TByteArrayList (gnu.trove.list.array.TByteArrayList)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 IOException (java.io.IOException)2 Node (net.osmand.osm.edit.Node)2 Way (net.osmand.osm.edit.Way)2 RunMiXCR (com.milaboratory.mixcr.util.RunMiXCR)1 TLongIterator (gnu.trove.iterator.TLongIterator)1 TDoubleArrayList (gnu.trove.list.array.TDoubleArrayList)1 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)1 TShortArrayList (gnu.trove.list.array.TShortArrayList)1 TLongHashSet (gnu.trove.set.hash.TLongHashSet)1