1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
@Component public class GeoFeatureManagementService { @Autowired private MongoTemplate mongoTemplate; @Autowired private GeoJSONSpatialAnalysisService spatialAnalysisService;
@PostConstruct public void configureGeoFeatureIndexes() { String collectionName = "geo_features"; try { Index geometryIndex = new Index() .on("geometry", Sort.Direction.ASC) .named("geo_features_geometry_2dsphere"); mongoTemplate.indexOps(collectionName).ensureIndex(geometryIndex); Index typeIndex = new Index() .on("properties.type", Sort.Direction.ASC) .named("geo_features_type"); mongoTemplate.indexOps(collectionName).ensureIndex(typeIndex); Index nameIndex = new Index() .on("properties.name", Sort.Direction.ASC) .named("geo_features_name"); mongoTemplate.indexOps(collectionName).ensureIndex(nameIndex); Index createdAtIndex = new Index() .on("createdAt", Sort.Direction.DESC) .named("geo_features_created_at"); mongoTemplate.indexOps(collectionName).ensureIndex(createdAtIndex); System.out.println("地理要素索引配置完成"); } catch (Exception e) { System.err.println("地理要素索引配置失败: " + e.getMessage()); } }
public void addGeoFeature(String name, String type, Document geometry, Map<String, Object> properties) { try { Document feature = new Document(); feature.put("type", "Feature"); feature.put("geometry", geometry); Document props = new Document(); props.put("name", name); props.put("type", type); if (properties != null) { props.putAll(properties); } feature.put("properties", props); feature.put("createdAt", new Date()); feature.put("updatedAt", new Date()); mongoTemplate.insert(feature, "geo_features"); System.out.println("成功添加地理要素: " + name); } catch (Exception e) { System.err.println("添加地理要素失败: " + e.getMessage()); } }
public List<Document> queryGeoFeatures(String type, Map<String, Double> bounds) { try { Query query = new Query(); if (type != null && !type.isEmpty()) { query.addCriteria(Criteria.where("properties.type").is(type)); } if (bounds != null) { double minLon = bounds.get("minLongitude"); double minLat = bounds.get("minLatitude"); double maxLon = bounds.get("maxLongitude"); double maxLat = bounds.get("maxLatitude"); Document box = new Document(); box.put("type", "Polygon"); box.put("coordinates", new Object[]{ new double[][]{ {minLon, minLat}, {maxLon, minLat}, {maxLon, maxLat}, {minLon, maxLat}, {minLon, minLat} } }); query.addCriteria(Criteria.where("geometry").within(box)); } List<Document> results = mongoTemplate.find(query, Document.class, "geo_features"); System.out.println("找到 " + results.size() + " 个地理要素"); return results; } catch (Exception e) { System.err.println("查询地理要素失败: " + e.getMessage()); return new ArrayList<>(); } }
public Map<String, Object> analyzeGeoFeature(String featureId) { try { Query query = new Query(); query.addCriteria(Criteria.where("_id").is(new ObjectId(featureId))); Document feature = mongoTemplate.findOne(query, Document.class, "geo_features"); if (feature == null) { return new HashMap<>(); } Document geometry = (Document) feature.get("geometry"); String geometryType = geometry.getString("type"); Map<String, Object> analysis = new HashMap<>(); analysis.put("type", geometryType); analysis.put("name", ((Document) feature.get("properties")).getString("name")); switch (geometryType) { case "Point": analysis.put("coordinates", geometry.get("coordinates")); break; case "LineString": analysis.put("length", spatialAnalysisService.calculateLength(geometry)); analysis.put("coordinates", geometry.get("coordinates")); break; case "Polygon": analysis.put("area", spatialAnalysisService.calculateArea(geometry)); analysis.put("coordinates", geometry.get("coordinates")); break; case "MultiPoint": analysis.put("pointCount", ((List<?>) geometry.get("coordinates")).size()); analysis.put("coordinates", geometry.get("coordinates")); break; case "MultiLineString": analysis.put("lineCount", ((List<?>) geometry.get("coordinates")).size()); analysis.put("totalLength", spatialAnalysisService.calculateLength(geometry)); analysis.put("coordinates", geometry.get("coordinates")); break; case "MultiPolygon": analysis.put("polygonCount", ((List<?>) geometry.get("coordinates")).size()); analysis.put("totalArea", spatialAnalysisService.calculateArea(geometry)); analysis.put("coordinates", geometry.get("coordinates")); break; } System.out.println("地理要素分析完成: " + featureId); return analysis; } catch (Exception e) { System.err.println("分析地理要素失败: " + e.getMessage()); return new HashMap<>(); } } }
|